Add .zfs control directory
[zfs.git] / module / zfs / zpl_super.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
23  */
24
25
26 #include <sys/zfs_vfsops.h>
27 #include <sys/zfs_vnops.h>
28 #include <sys/zfs_znode.h>
29 #include <sys/zfs_ctldir.h>
30 #include <sys/zpl.h>
31
32
33 static struct inode *
34 zpl_inode_alloc(struct super_block *sb)
35 {
36         struct inode *ip;
37
38         VERIFY3S(zfs_inode_alloc(sb, &ip), ==, 0);
39         ip->i_version = 1;
40
41         return (ip);
42 }
43
44 static void
45 zpl_inode_destroy(struct inode *ip)
46 {
47         ASSERT(atomic_read(&ip->i_count) == 0);
48         zfs_inode_destroy(ip);
49 }
50
51 /*
52  * When ->drop_inode() is called its return value indicates if the
53  * inode should be evicted from the inode cache.  If the inode is
54  * unhashed and has no links the default policy is to evict it
55  * immediately.
56  *
57  * Prior to 2.6.36 this eviction was accomplished by the vfs calling
58  * ->delete_inode().  It was ->delete_inode()'s responsibility to
59  * truncate the inode pages and call clear_inode().  The call to
60  * clear_inode() synchronously invalidates all the buffers and
61  * calls ->clear_inode().  It was ->clear_inode()'s responsibility
62  * to cleanup and filesystem specific data before freeing the inode.
63  *
64  * This elaborate mechanism was replaced by ->evict_inode() which
65  * does the job of both ->delete_inode() and ->clear_inode().  It
66  * will be called exactly once, and when it returns the inode must
67  * be in a state where it can simply be freed.  The ->evict_inode()
68  * callback must minimally truncate the inode pages, and call
69  * end_writeback() to complete all outstanding writeback for the
70  * inode.  After this is complete evict inode can cleanup any
71  * remaining filesystem specific data.
72  */
73 #ifdef HAVE_EVICT_INODE
74 static void
75 zpl_evict_inode(struct inode *ip)
76 {
77         truncate_setsize(ip, 0);
78         end_writeback(ip);
79         zfs_inactive(ip);
80 }
81
82 #else
83
84 static void
85 zpl_clear_inode(struct inode *ip)
86 {
87         zfs_inactive(ip);
88 }
89
90 static void
91 zpl_inode_delete(struct inode *ip)
92 {
93         truncate_setsize(ip, 0);
94         clear_inode(ip);
95 }
96
97 #endif /* HAVE_EVICT_INODE */
98
99 static void
100 zpl_put_super(struct super_block *sb)
101 {
102         int error;
103
104         error = -zfs_umount(sb);
105         ASSERT3S(error, <=, 0);
106 }
107
108 static int
109 zpl_sync_fs(struct super_block *sb, int wait)
110 {
111         cred_t *cr = CRED();
112         int error;
113
114         crhold(cr);
115         error = -zfs_sync(sb, wait, cr);
116         crfree(cr);
117         ASSERT3S(error, <=, 0);
118
119         return (error);
120 }
121
122 static int
123 zpl_statfs(struct dentry *dentry, struct kstatfs *statp)
124 {
125         int error;
126
127         error = -zfs_statvfs(dentry, statp);
128         ASSERT3S(error, <=, 0);
129
130         return (error);
131 }
132
133 static int
134 zpl_remount_fs(struct super_block *sb, int *flags, char *data)
135 {
136         int error;
137         error = -zfs_remount(sb, flags, data);
138         ASSERT3S(error, <=, 0);
139
140         return (error);
141 }
142
143 static void
144 zpl_umount_begin(struct super_block *sb)
145 {
146         zfs_sb_t *zsb = sb->s_fs_info;
147         int count;
148
149         /*
150          * Best effort to unmount snapshots in .zfs/snapshot/.  Normally this
151          * isn't required because snapshots have the MNT_SHRINKABLE flag set.
152          */
153         if (zsb->z_ctldir)
154                 (void) zfsctl_unmount_snapshots(zsb, MNT_FORCE, &count);
155 }
156
157 /*
158  * The Linux VFS automatically handles the following flags:
159  * MNT_NOSUID, MNT_NODEV, MNT_NOEXEC, MNT_NOATIME, MNT_READONLY
160  */
161 #ifdef HAVE_SHOW_OPTIONS_WITH_DENTRY
162 static int
163 zpl_show_options(struct seq_file *seq, struct dentry *root)
164 {
165         zfs_sb_t *zsb = root->d_sb->s_fs_info;
166
167         seq_printf(seq, ",%s", zsb->z_flags & ZSB_XATTR ? "xattr" : "noxattr");
168
169         return (0);
170 }
171 #else
172 static int
173 zpl_show_options(struct seq_file *seq, struct vfsmount *vfsp)
174 {
175         zfs_sb_t *zsb = vfsp->mnt_sb->s_fs_info;
176
177         seq_printf(seq, ",%s", zsb->z_flags & ZSB_XATTR ? "xattr" : "noxattr");
178
179         return (0);
180 }
181 #endif /* HAVE_SHOW_OPTIONS_WITH_DENTRY */
182
183 static int
184 zpl_fill_super(struct super_block *sb, void *data, int silent)
185 {
186         int error;
187
188         error = -zfs_domount(sb, data, silent);
189         ASSERT3S(error, <=, 0);
190
191         return (error);
192 }
193
194 #ifdef HAVE_MOUNT_NODEV
195 static struct dentry *
196 zpl_mount(struct file_system_type *fs_type, int flags,
197     const char *osname, void *data)
198 {
199         zpl_mount_data_t zmd = { osname, data };
200
201         return mount_nodev(fs_type, flags, &zmd, zpl_fill_super);
202 }
203 #else
204 static int
205 zpl_get_sb(struct file_system_type *fs_type, int flags,
206     const char *osname, void *data, struct vfsmount *mnt)
207 {
208         zpl_mount_data_t zmd = { osname, data };
209
210         return get_sb_nodev(fs_type, flags, &zmd, zpl_fill_super, mnt);
211 }
212 #endif /* HAVE_MOUNT_NODEV */
213
214 static void
215 zpl_kill_sb(struct super_block *sb)
216 {
217         zfs_preumount(sb);
218         kill_anon_super(sb);
219 }
220
221 #ifdef HAVE_SHRINK
222 /*
223  * Linux 3.1 - 3.x API
224  *
225  * The Linux 3.1 API introduced per-sb cache shrinkers to replace the
226  * global ones.  This allows us a mechanism to cleanly target a specific
227  * zfs file system when the dnode and inode caches grow too large.
228  *
229  * In addition, the 3.0 kernel added the iterate_supers_type() helper
230  * function which is used to safely walk all of the zfs file systems.
231  */
232 static void
233 zpl_prune_sb(struct super_block *sb, void *arg)
234 {
235         int objects = 0;
236         int error;
237
238         error = -zfs_sb_prune(sb, *(unsigned long *)arg, &objects);
239         ASSERT3S(error, <=, 0);
240
241         return;
242 }
243
244 void
245 zpl_prune_sbs(int64_t bytes_to_scan, void *private)
246 {
247         unsigned long nr_to_scan = (bytes_to_scan / sizeof(znode_t));
248
249         iterate_supers_type(&zpl_fs_type, zpl_prune_sb, &nr_to_scan);
250         kmem_reap();
251 }
252 #else
253 /*
254  * Linux 2.6.x - 3.0 API
255  *
256  * These are best effort interfaces are provided by the SPL to induce
257  * the Linux VM subsystem to reclaim a fraction of the both dnode and
258  * inode caches.  Ideally, we want to just target the zfs file systems
259  * however our only option is to reclaim from them all.
260  */
261 void
262 zpl_prune_sbs(int64_t bytes_to_scan, void *private)
263 {
264         unsigned long nr_to_scan = (bytes_to_scan / sizeof(znode_t));
265
266         shrink_dcache_memory(nr_to_scan, GFP_KERNEL);
267         shrink_icache_memory(nr_to_scan, GFP_KERNEL);
268         kmem_reap();
269 }
270 #endif /* HAVE_SHRINK */
271
272 #ifdef HAVE_NR_CACHED_OBJECTS
273 static int
274 zpl_nr_cached_objects(struct super_block *sb)
275 {
276         zfs_sb_t *zsb = sb->s_fs_info;
277         int nr;
278
279         mutex_enter(&zsb->z_znodes_lock);
280         nr = zsb->z_nr_znodes;
281         mutex_exit(&zsb->z_znodes_lock);
282
283         return (nr);
284 }
285 #endif /* HAVE_NR_CACHED_OBJECTS */
286
287 #ifdef HAVE_FREE_CACHED_OBJECTS
288 /*
289  * Attempt to evict some meta data from the cache.  The ARC operates in
290  * terms of bytes while the Linux VFS uses objects.  Now because this is
291  * just a best effort eviction and the exact values aren't critical so we
292  * extrapolate from an object count to a byte size using the znode_t size.
293  */
294 static void
295 zpl_free_cached_objects(struct super_block *sb, int nr_to_scan)
296 {
297         arc_adjust_meta(nr_to_scan * sizeof(znode_t), B_FALSE);
298 }
299 #endif /* HAVE_FREE_CACHED_OBJECTS */
300
301 const struct super_operations zpl_super_operations = {
302         .alloc_inode            = zpl_inode_alloc,
303         .destroy_inode          = zpl_inode_destroy,
304         .dirty_inode            = NULL,
305         .write_inode            = NULL,
306         .drop_inode             = NULL,
307 #ifdef HAVE_EVICT_INODE
308         .evict_inode            = zpl_evict_inode,
309 #else
310         .clear_inode            = zpl_clear_inode,
311         .delete_inode           = zpl_inode_delete,
312 #endif /* HAVE_EVICT_INODE */
313         .put_super              = zpl_put_super,
314         .write_super            = NULL,
315         .sync_fs                = zpl_sync_fs,
316         .statfs                 = zpl_statfs,
317         .remount_fs             = zpl_remount_fs,
318         .umount_begin           = zpl_umount_begin,
319         .show_options           = zpl_show_options,
320         .show_stats             = NULL,
321 #ifdef HAVE_NR_CACHED_OBJECTS
322         .nr_cached_objects      = zpl_nr_cached_objects,
323 #endif /* HAVE_NR_CACHED_OBJECTS */
324 #ifdef HAVE_FREE_CACHED_OBJECTS
325         .free_cached_objects    = zpl_free_cached_objects,
326 #endif /* HAVE_FREE_CACHED_OBJECTS */
327 };
328
329 struct file_system_type zpl_fs_type = {
330         .owner                  = THIS_MODULE,
331         .name                   = ZFS_DRIVER,
332 #ifdef HAVE_MOUNT_NODEV
333         .mount                  = zpl_mount,
334 #else
335         .get_sb                 = zpl_get_sb,
336 #endif /* HAVE_MOUNT_NODEV */
337         .kill_sb                = zpl_kill_sb,
338 };