]> granicus.if.org Git - zfs/blob - module/zfs/zpl_super.c
Honor xattr=sa dataset property
[zfs] / 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  * Called from __mark_inode_dirty() to reflect that something in the
53  * inode has changed.  We use it to ensure the znode system attributes
54  * are always strictly update to date with respect to the inode.
55  */
56 #ifdef HAVE_DIRTY_INODE_WITH_FLAGS
57 static void
58 zpl_dirty_inode(struct inode *ip, int flags)
59 {
60         fstrans_cookie_t cookie;
61
62         cookie = spl_fstrans_mark();
63         zfs_dirty_inode(ip, flags);
64         spl_fstrans_unmark(cookie);
65 }
66 #else
67 static void
68 zpl_dirty_inode(struct inode *ip)
69 {
70         fstrans_cookie_t cookie;
71
72         cookie = spl_fstrans_mark();
73         zfs_dirty_inode(ip, 0);
74         spl_fstrans_unmark(cookie);
75 }
76 #endif /* HAVE_DIRTY_INODE_WITH_FLAGS */
77
78 /*
79  * When ->drop_inode() is called its return value indicates if the
80  * inode should be evicted from the inode cache.  If the inode is
81  * unhashed and has no links the default policy is to evict it
82  * immediately.
83  *
84  * Prior to 2.6.36 this eviction was accomplished by the vfs calling
85  * ->delete_inode().  It was ->delete_inode()'s responsibility to
86  * truncate the inode pages and call clear_inode().  The call to
87  * clear_inode() synchronously invalidates all the buffers and
88  * calls ->clear_inode().  It was ->clear_inode()'s responsibility
89  * to cleanup and filesystem specific data before freeing the inode.
90  *
91  * This elaborate mechanism was replaced by ->evict_inode() which
92  * does the job of both ->delete_inode() and ->clear_inode().  It
93  * will be called exactly once, and when it returns the inode must
94  * be in a state where it can simply be freed.i
95  *
96  * The ->evict_inode() callback must minimally truncate the inode pages,
97  * and call clear_inode().  For 2.6.35 and later kernels this will
98  * simply update the inode state, with the sync occurring before the
99  * truncate in evict().  For earlier kernels clear_inode() maps to
100  * end_writeback() which is responsible for completing all outstanding
101  * write back.  In either case, once this is done it is safe to cleanup
102  * any remaining inode specific data via zfs_inactive().
103  * remaining filesystem specific data.
104  */
105 #ifdef HAVE_EVICT_INODE
106 static void
107 zpl_evict_inode(struct inode *ip)
108 {
109         fstrans_cookie_t cookie;
110
111         cookie = spl_fstrans_mark();
112         truncate_setsize(ip, 0);
113         clear_inode(ip);
114         zfs_inactive(ip);
115         spl_fstrans_unmark(cookie);
116 }
117
118 #else
119
120 static void
121 zpl_drop_inode(struct inode *ip)
122 {
123         generic_delete_inode(ip);
124 }
125
126 static void
127 zpl_clear_inode(struct inode *ip)
128 {
129         fstrans_cookie_t cookie;
130
131         cookie = spl_fstrans_mark();
132         zfs_inactive(ip);
133         spl_fstrans_unmark(cookie);
134 }
135
136 static void
137 zpl_inode_delete(struct inode *ip)
138 {
139         truncate_setsize(ip, 0);
140         clear_inode(ip);
141 }
142 #endif /* HAVE_EVICT_INODE */
143
144 static void
145 zpl_put_super(struct super_block *sb)
146 {
147         fstrans_cookie_t cookie;
148         int error;
149
150         cookie = spl_fstrans_mark();
151         error = -zfs_umount(sb);
152         spl_fstrans_unmark(cookie);
153         ASSERT3S(error, <=, 0);
154 }
155
156 static int
157 zpl_sync_fs(struct super_block *sb, int wait)
158 {
159         fstrans_cookie_t cookie;
160         cred_t *cr = CRED();
161         int error;
162
163         crhold(cr);
164         cookie = spl_fstrans_mark();
165         error = -zfs_sync(sb, wait, cr);
166         spl_fstrans_unmark(cookie);
167         crfree(cr);
168         ASSERT3S(error, <=, 0);
169
170         return (error);
171 }
172
173 static int
174 zpl_statfs(struct dentry *dentry, struct kstatfs *statp)
175 {
176         fstrans_cookie_t cookie;
177         int error;
178
179         cookie = spl_fstrans_mark();
180         error = -zfs_statvfs(dentry, statp);
181         spl_fstrans_unmark(cookie);
182         ASSERT3S(error, <=, 0);
183
184         return (error);
185 }
186
187 enum {
188         TOKEN_RO,
189         TOKEN_RW,
190         TOKEN_SETUID,
191         TOKEN_NOSETUID,
192         TOKEN_EXEC,
193         TOKEN_NOEXEC,
194         TOKEN_DEVICES,
195         TOKEN_NODEVICES,
196         TOKEN_DIRXATTR,
197         TOKEN_SAXATTR,
198         TOKEN_XATTR,
199         TOKEN_NOXATTR,
200         TOKEN_ATIME,
201         TOKEN_NOATIME,
202         TOKEN_RELATIME,
203         TOKEN_NORELATIME,
204         TOKEN_NBMAND,
205         TOKEN_NONBMAND,
206         TOKEN_MNTPOINT,
207         TOKEN_LAST,
208 };
209
210 static const match_table_t zpl_tokens = {
211         { TOKEN_RO,             MNTOPT_RO },
212         { TOKEN_RW,             MNTOPT_RW },
213         { TOKEN_SETUID,         MNTOPT_SETUID },
214         { TOKEN_NOSETUID,       MNTOPT_NOSETUID },
215         { TOKEN_EXEC,           MNTOPT_EXEC },
216         { TOKEN_NOEXEC,         MNTOPT_NOEXEC },
217         { TOKEN_DEVICES,        MNTOPT_DEVICES },
218         { TOKEN_NODEVICES,      MNTOPT_NODEVICES },
219         { TOKEN_DIRXATTR,       MNTOPT_DIRXATTR },
220         { TOKEN_SAXATTR,        MNTOPT_SAXATTR },
221         { TOKEN_XATTR,          MNTOPT_XATTR },
222         { TOKEN_NOXATTR,        MNTOPT_NOXATTR },
223         { TOKEN_ATIME,          MNTOPT_ATIME },
224         { TOKEN_NOATIME,        MNTOPT_NOATIME },
225         { TOKEN_RELATIME,       MNTOPT_RELATIME },
226         { TOKEN_NORELATIME,     MNTOPT_NORELATIME },
227         { TOKEN_NBMAND,         MNTOPT_NBMAND },
228         { TOKEN_NONBMAND,       MNTOPT_NONBMAND },
229         { TOKEN_MNTPOINT,       MNTOPT_MNTPOINT "=%s" },
230         { TOKEN_LAST,           NULL },
231 };
232
233 static int
234 zpl_parse_option(char *option, int token, substring_t *args, zfs_mntopts_t *zmo)
235 {
236         switch (token) {
237         case TOKEN_RO:
238                 zmo->z_readonly = B_TRUE;
239                 zmo->z_do_readonly = B_TRUE;
240                 break;
241         case TOKEN_RW:
242                 zmo->z_readonly = B_FALSE;
243                 zmo->z_do_readonly = B_TRUE;
244                 break;
245         case TOKEN_SETUID:
246                 zmo->z_setuid = B_TRUE;
247                 zmo->z_do_setuid = B_TRUE;
248                 break;
249         case TOKEN_NOSETUID:
250                 zmo->z_setuid = B_FALSE;
251                 zmo->z_do_setuid = B_TRUE;
252                 break;
253         case TOKEN_EXEC:
254                 zmo->z_exec = B_TRUE;
255                 zmo->z_do_exec = B_TRUE;
256                 break;
257         case TOKEN_NOEXEC:
258                 zmo->z_exec = B_FALSE;
259                 zmo->z_do_exec = B_TRUE;
260                 break;
261         case TOKEN_DEVICES:
262                 zmo->z_devices = B_TRUE;
263                 zmo->z_do_devices = B_TRUE;
264                 break;
265         case TOKEN_NODEVICES:
266                 zmo->z_devices = B_FALSE;
267                 zmo->z_do_devices = B_TRUE;
268                 break;
269         case TOKEN_DIRXATTR:
270                 zmo->z_xattr = ZFS_XATTR_DIR;
271                 zmo->z_do_xattr = B_TRUE;
272                 break;
273         case TOKEN_SAXATTR:
274                 zmo->z_xattr = ZFS_XATTR_SA;
275                 zmo->z_do_xattr = B_TRUE;
276                 break;
277         case TOKEN_XATTR:
278                 zmo->z_xattr = ZFS_XATTR_DIR;
279                 zmo->z_do_xattr = B_TRUE;
280                 break;
281         case TOKEN_NOXATTR:
282                 zmo->z_xattr = ZFS_XATTR_OFF;
283                 zmo->z_do_xattr = B_TRUE;
284                 break;
285         case TOKEN_ATIME:
286                 zmo->z_atime = B_TRUE;
287                 zmo->z_do_atime = B_TRUE;
288                 break;
289         case TOKEN_NOATIME:
290                 zmo->z_atime = B_FALSE;
291                 zmo->z_do_atime = B_TRUE;
292                 break;
293         case TOKEN_RELATIME:
294                 zmo->z_relatime = B_TRUE;
295                 zmo->z_do_relatime = B_TRUE;
296                 break;
297         case TOKEN_NORELATIME:
298                 zmo->z_relatime = B_FALSE;
299                 zmo->z_do_relatime = B_TRUE;
300                 break;
301         case TOKEN_NBMAND:
302                 zmo->z_nbmand = B_TRUE;
303                 zmo->z_do_nbmand = B_TRUE;
304                 break;
305         case TOKEN_NONBMAND:
306                 zmo->z_nbmand = B_FALSE;
307                 zmo->z_do_nbmand = B_TRUE;
308                 break;
309         case TOKEN_MNTPOINT:
310                 zmo->z_mntpoint = match_strdup(&args[0]);
311                 if (zmo->z_mntpoint == NULL)
312                         return (-ENOMEM);
313
314                 break;
315         default:
316                 break;
317         }
318
319         return (0);
320 }
321
322 /*
323  * Parse the mntopts string storing the results in provided zmo argument.
324  * If an error occurs the zmo argument will not be modified.  The caller
325  * needs to set isremount when recycling an existing zfs_mntopts_t.
326  */
327 static int
328 zpl_parse_options(char *osname, char *mntopts, zfs_mntopts_t *zmo,
329     boolean_t isremount)
330 {
331         zfs_mntopts_t *tmp_zmo;
332         int error;
333
334         tmp_zmo = zfs_mntopts_alloc();
335         tmp_zmo->z_osname = strdup(osname);
336
337         if (mntopts) {
338                 substring_t args[MAX_OPT_ARGS];
339                 char *tmp_mntopts, *p;
340                 int token;
341
342                 tmp_mntopts = strdup(mntopts);
343
344                 while ((p = strsep(&tmp_mntopts, ",")) != NULL) {
345                         if (!*p)
346                                 continue;
347
348                         args[0].to = args[0].from = NULL;
349                         token = match_token(p, zpl_tokens, args);
350                         error = zpl_parse_option(p, token, args, tmp_zmo);
351                         if (error) {
352                                 zfs_mntopts_free(tmp_zmo);
353                                 strfree(tmp_mntopts);
354                                 return (error);
355                         }
356                 }
357
358                 strfree(tmp_mntopts);
359         }
360
361         if (isremount == B_TRUE) {
362                 if (zmo->z_osname)
363                         strfree(zmo->z_osname);
364
365                 if (zmo->z_mntpoint)
366                         strfree(zmo->z_mntpoint);
367         } else {
368                 ASSERT3P(zmo->z_osname, ==, NULL);
369                 ASSERT3P(zmo->z_mntpoint, ==, NULL);
370         }
371
372         memcpy(zmo, tmp_zmo, sizeof (zfs_mntopts_t));
373         kmem_free(tmp_zmo, sizeof (zfs_mntopts_t));
374
375         return (0);
376 }
377
378 static int
379 zpl_remount_fs(struct super_block *sb, int *flags, char *data)
380 {
381         zfs_sb_t *zsb = sb->s_fs_info;
382         fstrans_cookie_t cookie;
383         int error;
384
385         error = zpl_parse_options(zsb->z_mntopts->z_osname, data,
386             zsb->z_mntopts, B_TRUE);
387         if (error)
388                 return (error);
389
390         cookie = spl_fstrans_mark();
391         error = -zfs_remount(sb, flags, zsb->z_mntopts);
392         spl_fstrans_unmark(cookie);
393         ASSERT3S(error, <=, 0);
394
395         return (error);
396 }
397
398 static int
399 __zpl_show_options(struct seq_file *seq, zfs_sb_t *zsb)
400 {
401         seq_printf(seq, ",%s", zsb->z_flags & ZSB_XATTR ? "xattr" : "noxattr");
402
403 #ifdef CONFIG_FS_POSIX_ACL
404         switch (zsb->z_acl_type) {
405         case ZFS_ACLTYPE_POSIXACL:
406                 seq_puts(seq, ",posixacl");
407                 break;
408         default:
409                 seq_puts(seq, ",noacl");
410                 break;
411         }
412 #endif /* CONFIG_FS_POSIX_ACL */
413
414         return (0);
415 }
416
417 #ifdef HAVE_SHOW_OPTIONS_WITH_DENTRY
418 static int
419 zpl_show_options(struct seq_file *seq, struct dentry *root)
420 {
421         return (__zpl_show_options(seq, root->d_sb->s_fs_info));
422 }
423 #else
424 static int
425 zpl_show_options(struct seq_file *seq, struct vfsmount *vfsp)
426 {
427         return (__zpl_show_options(seq, vfsp->mnt_sb->s_fs_info));
428 }
429 #endif /* HAVE_SHOW_OPTIONS_WITH_DENTRY */
430
431 static int
432 zpl_fill_super(struct super_block *sb, void *data, int silent)
433 {
434         zfs_mntopts_t *zmo = (zfs_mntopts_t *)data;
435         fstrans_cookie_t cookie;
436         int error;
437
438         cookie = spl_fstrans_mark();
439         error = -zfs_domount(sb, zmo, silent);
440         spl_fstrans_unmark(cookie);
441         ASSERT3S(error, <=, 0);
442
443         return (error);
444 }
445
446 #ifdef HAVE_MOUNT_NODEV
447 static struct dentry *
448 zpl_mount(struct file_system_type *fs_type, int flags,
449     const char *osname, void *data)
450 {
451         zfs_mntopts_t *zmo = zfs_mntopts_alloc();
452         int error;
453
454         error = zpl_parse_options((char *)osname, (char *)data, zmo, B_FALSE);
455         if (error) {
456                 zfs_mntopts_free(zmo);
457                 return (ERR_PTR(error));
458         }
459
460         return (mount_nodev(fs_type, flags, zmo, zpl_fill_super));
461 }
462 #else
463 static int
464 zpl_get_sb(struct file_system_type *fs_type, int flags,
465     const char *osname, void *data, struct vfsmount *mnt)
466 {
467         zfs_mntopts_t *zmo = zfs_mntopts_alloc();
468         int error;
469
470         error = zpl_parse_options((char *)osname, (char *)data, zmo, B_FALSE);
471         if (error) {
472                 zfs_mntopts_free(zmo);
473                 return (error);
474         }
475
476         return (get_sb_nodev(fs_type, flags, zmo, zpl_fill_super, mnt));
477 }
478 #endif /* HAVE_MOUNT_NODEV */
479
480 static void
481 zpl_kill_sb(struct super_block *sb)
482 {
483         zfs_preumount(sb);
484         kill_anon_super(sb);
485
486 #ifdef HAVE_S_INSTANCES_LIST_HEAD
487         sb->s_instances.next = &(zpl_fs_type.fs_supers);
488 #endif /* HAVE_S_INSTANCES_LIST_HEAD */
489 }
490
491 void
492 zpl_prune_sb(int64_t nr_to_scan, void *arg)
493 {
494         struct super_block *sb = (struct super_block *)arg;
495         int objects = 0;
496
497         (void) -zfs_sb_prune(sb, nr_to_scan, &objects);
498 }
499
500 #ifdef HAVE_NR_CACHED_OBJECTS
501 static int
502 zpl_nr_cached_objects(struct super_block *sb)
503 {
504         zfs_sb_t *zsb = sb->s_fs_info;
505         int nr;
506
507         mutex_enter(&zsb->z_znodes_lock);
508         nr = zsb->z_nr_znodes;
509         mutex_exit(&zsb->z_znodes_lock);
510
511         return (nr);
512 }
513 #endif /* HAVE_NR_CACHED_OBJECTS */
514
515 #ifdef HAVE_FREE_CACHED_OBJECTS
516 /*
517  * Attempt to evict some meta data from the cache.  The ARC operates in
518  * terms of bytes while the Linux VFS uses objects.  Now because this is
519  * just a best effort eviction and the exact values aren't critical so we
520  * extrapolate from an object count to a byte size using the znode_t size.
521  */
522 static void
523 zpl_free_cached_objects(struct super_block *sb, int nr_to_scan)
524 {
525         /* noop */
526 }
527 #endif /* HAVE_FREE_CACHED_OBJECTS */
528
529 const struct super_operations zpl_super_operations = {
530         .alloc_inode            = zpl_inode_alloc,
531         .destroy_inode          = zpl_inode_destroy,
532         .dirty_inode            = zpl_dirty_inode,
533         .write_inode            = NULL,
534 #ifdef HAVE_EVICT_INODE
535         .evict_inode            = zpl_evict_inode,
536 #else
537         .drop_inode             = zpl_drop_inode,
538         .clear_inode            = zpl_clear_inode,
539         .delete_inode           = zpl_inode_delete,
540 #endif /* HAVE_EVICT_INODE */
541         .put_super              = zpl_put_super,
542         .sync_fs                = zpl_sync_fs,
543         .statfs                 = zpl_statfs,
544         .remount_fs             = zpl_remount_fs,
545         .show_options           = zpl_show_options,
546         .show_stats             = NULL,
547 #ifdef HAVE_NR_CACHED_OBJECTS
548         .nr_cached_objects      = zpl_nr_cached_objects,
549 #endif /* HAVE_NR_CACHED_OBJECTS */
550 #ifdef HAVE_FREE_CACHED_OBJECTS
551         .free_cached_objects    = zpl_free_cached_objects,
552 #endif /* HAVE_FREE_CACHED_OBJECTS */
553 };
554
555 struct file_system_type zpl_fs_type = {
556         .owner                  = THIS_MODULE,
557         .name                   = ZFS_DRIVER,
558 #ifdef HAVE_MOUNT_NODEV
559         .mount                  = zpl_mount,
560 #else
561         .get_sb                 = zpl_get_sb,
562 #endif /* HAVE_MOUNT_NODEV */
563         .kill_sb                = zpl_kill_sb,
564 };