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