1 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
2 index 31317c9..39b31aa 100644
5 @@ -398,6 +398,13 @@ struct flex_groups {
6 #define EXT4_FL_USER_VISIBLE 0x304BDFFF /* User visible flags */
7 #define EXT4_FL_USER_MODIFIABLE 0x204380FF /* User modifiable flags */
9 +#define EXT4_FL_XFLAG_VISIBLE (EXT4_SYNC_FL | \
10 + EXT4_IMMUTABLE_FL | \
14 + EXT4_PROJINHERIT_FL)
16 /* Flags that should be inherited by new inodes from their parent. */
17 #define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL |\
18 EXT4_SYNC_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL |\
19 @@ -621,6 +628,44 @@ enum {
20 #define EXT4_IOC_SWAP_BOOT _IO('f', 17)
21 #define EXT4_IOC_PRECACHE_EXTENTS _IO('f', 18)
23 +#ifndef FS_IOC_FSGETXATTR
24 +/* Until the uapi changes get merged for project quota... */
25 +#define FS_IOC_FSGETXATTR _IOR('X', 31, struct fsxattr)
26 +#define FS_IOC_FSSETXATTR _IOW('X', 32, struct fsxattr)
28 + * Structure for FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR.
31 + __u32 fsx_xflags; /* xflags field value (get/set) */
32 + __u32 fsx_extsize; /* extsize field value (get/set)*/
33 + __u32 fsx_nextents; /* nextents field value (get) */
34 + __u32 fsx_projid; /* project identifier (get/set) */
35 + unsigned char fsx_pad[12];
39 + * Flags for the fsx_xflags field
41 +#define FS_XFLAG_REALTIME 0x00000001 /* data in realtime volume */
42 +#define FS_XFLAG_PREALLOC 0x00000002 /* preallocated file extents */
43 +#define FS_XFLAG_IMMUTABLE 0x00000008 /* file cannot be modified */
44 +#define FS_XFLAG_APPEND 0x00000010 /* all writes append */
45 +#define FS_XFLAG_SYNC 0x00000020 /* all writes synchronous */
46 +#define FS_XFLAG_NOATIME 0x00000040 /* do not update access time */
47 +#define FS_XFLAG_NODUMP 0x00000080 /* do not include in backups */
48 +#define FS_XFLAG_RTINHERIT 0x00000100 /* create with rt bit set */
49 +#define FS_XFLAG_PROJINHERIT 0x00000200 /* create with parents projid */
50 +#define FS_XFLAG_NOSYMLINKS 0x00000400 /* disallow symlink creation */
51 +#define FS_XFLAG_EXTSIZE 0x00000800 /* extent size allocator hint */
52 +#define FS_XFLAG_EXTSZINHERIT 0x00001000 /* inherit inode extent size */
53 +#define FS_XFLAG_NODEFRAG 0x00002000 /* do not defragment */
54 +#define FS_XFLAG_FILESTREAM 0x00004000 /* use filestream allocator */
55 +#define FS_XFLAG_HASATTR 0x80000000 /* no DIFLAG for this */
56 +#endif /* !defined(FS_IOC_FSGETXATTR) */
58 +#define EXT4_IOC_FSGETXATTR FS_IOC_FSGETXATTR
59 +#define EXT4_IOC_FSSETXATTR FS_IOC_FSSETXATTR
61 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
63 * ioctl commands in 32 bit emulation
64 @@ -2336,6 +2381,7 @@ extern int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
66 extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
67 extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
68 +extern int __ext4_ioctl_setproject(struct inode *inode, __u32 projid);
71 extern int ext4_ext_migrate(struct inode *);
72 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
73 index bfda18a..a4443e9 100644
77 #include <linux/mount.h>
78 #include <linux/file.h>
79 #include <asm/uaccess.h>
80 +#include <linux/quotaops.h>
81 #include "ext4_jbd2.h"
84 @@ -198,6 +199,251 @@ journal_err_out:
88 +static int ext4_ioctl_setflags(struct inode *inode,
91 + struct ext4_inode_info *ei = EXT4_I(inode);
92 + handle_t *handle = NULL;
93 + int err = EPERM, migrate = 0;
94 + struct ext4_iloc iloc;
95 + unsigned int oldflags, mask, i;
98 + /* Is it quota file? Do not allow user to mess with it */
99 + if (IS_NOQUOTA(inode))
102 + oldflags = ei->i_flags;
104 + /* The JOURNAL_DATA flag is modifiable only by root */
105 + jflag = flags & EXT4_JOURNAL_DATA_FL;
108 + * The IMMUTABLE and APPEND_ONLY flags can only be changed by
109 + * the relevant capability.
111 + * This test looks nicer. Thanks to Pauline Middelink
113 + if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
114 + if (!capable(CAP_LINUX_IMMUTABLE))
119 + * The JOURNAL_DATA flag can only be changed by
120 + * the relevant capability.
122 + if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
123 + if (!capable(CAP_SYS_RESOURCE))
126 + if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
129 + if (flags & EXT4_EOFBLOCKS_FL) {
130 + /* we don't support adding EOFBLOCKS flag */
131 + if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
135 + } else if (oldflags & EXT4_EOFBLOCKS_FL)
136 + ext4_truncate(inode);
138 + handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
139 + if (IS_ERR(handle)) {
140 + err = PTR_ERR(handle);
143 + if (IS_SYNC(inode))
144 + ext4_handle_sync(handle);
145 + err = ext4_reserve_inode_write(handle, inode, &iloc);
149 + for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
150 + if (!(mask & EXT4_FL_USER_MODIFIABLE))
153 + ext4_set_inode_flag(inode, i);
155 + ext4_clear_inode_flag(inode, i);
158 + ext4_set_inode_flags(inode);
159 + inode->i_ctime = ext4_current_time(inode);
161 + err = ext4_mark_iloc_dirty(handle, inode, &iloc);
163 + ext4_journal_stop(handle);
167 + if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
168 + err = ext4_change_inode_journal_flag(inode, jflag);
172 + if (flags & EXT4_EXTENTS_FL)
173 + err = ext4_ext_migrate(inode);
175 + err = ext4_ind_migrate(inode);
183 +int __ext4_ioctl_setproject(struct inode *inode, __u32 projid)
185 + struct super_block *sb = inode->i_sb;
186 + struct ext4_inode_info *ei = EXT4_I(inode);
190 + struct ext4_iloc iloc;
191 + struct ext4_inode *raw_inode;
192 + struct dquot *transfer_to[EXT4_MAXQUOTAS] = { };
194 + if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
195 + EXT4_FEATURE_RO_COMPAT_PROJECT)) {
196 + BUG_ON(__kprojid_val(EXT4_I(inode)->i_projid)
197 + != EXT4_DEF_PROJID);
198 + if (projid != EXT4_DEF_PROJID)
199 + return -EOPNOTSUPP;
204 + if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
205 + return -EOPNOTSUPP;
207 + kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
209 + if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
213 + mutex_lock(&inode->i_mutex);
214 + /* Is it quota file? Do not allow user to mess with it */
215 + if (IS_NOQUOTA(inode))
218 + err = ext4_get_inode_loc(inode, &iloc);
222 + raw_inode = ext4_raw_inode(&iloc);
223 + if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
230 + dquot_initialize(inode);
232 + handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
233 + EXT4_QUOTA_INIT_BLOCKS(sb) +
234 + EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
235 + if (IS_ERR(handle)) {
236 + err = PTR_ERR(handle);
240 + err = ext4_reserve_inode_write(handle, inode, &iloc);
244 + transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
245 + if (transfer_to[PRJQUOTA]) {
246 + err = __dquot_transfer(inode, transfer_to);
247 + dqput(transfer_to[PRJQUOTA]);
252 + EXT4_I(inode)->i_projid = kprojid;
253 + inode->i_ctime = ext4_current_time(inode);
255 + rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
259 + ext4_journal_stop(handle);
261 + mutex_unlock(&inode->i_mutex);
266 +EXPORT_SYMBOL(__ext4_ioctl_setproject);
268 +static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
271 + struct inode *inode = file_inode(filp);
273 + err = mnt_want_write_file(filp);
277 + err = __ext4_ioctl_setproject(inode, projid);
279 + mnt_drop_write_file(filp);
283 +static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
285 + if (projid != EXT4_DEF_PROJID)
286 + return -EOPNOTSUPP;
292 +/* Transfer internal flags to xflags */
293 +static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
297 + if (iflags & EXT4_SYNC_FL)
298 + xflags |= FS_XFLAG_SYNC;
299 + if (iflags & EXT4_IMMUTABLE_FL)
300 + xflags |= FS_XFLAG_IMMUTABLE;
301 + if (iflags & EXT4_APPEND_FL)
302 + xflags |= FS_XFLAG_APPEND;
303 + if (iflags & EXT4_NODUMP_FL)
304 + xflags |= FS_XFLAG_NODUMP;
305 + if (iflags & EXT4_NOATIME_FL)
306 + xflags |= FS_XFLAG_NOATIME;
307 + if (iflags & EXT4_PROJINHERIT_FL)
308 + xflags |= FS_XFLAG_PROJINHERIT;
312 +/* Transfer xflags flags to internal */
313 +static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
315 + unsigned long iflags = 0;
317 + if (xflags & FS_XFLAG_SYNC)
318 + iflags |= EXT4_SYNC_FL;
319 + if (xflags & FS_XFLAG_IMMUTABLE)
320 + iflags |= EXT4_IMMUTABLE_FL;
321 + if (xflags & FS_XFLAG_APPEND)
322 + iflags |= EXT4_APPEND_FL;
323 + if (xflags & FS_XFLAG_NODUMP)
324 + iflags |= EXT4_NODUMP_FL;
325 + if (xflags & FS_XFLAG_NOATIME)
326 + iflags |= EXT4_NOATIME_FL;
327 + if (xflags & FS_XFLAG_PROJINHERIT)
328 + iflags |= EXT4_PROJINHERIT_FL;
333 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
335 struct inode *inode = file_inode(filp);
336 @@ -213,11 +459,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
337 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
338 return put_user(flags, (int __user *) arg);
339 case EXT4_IOC_SETFLAGS: {
340 - handle_t *handle = NULL;
341 - int err, migrate = 0;
342 - struct ext4_iloc iloc;
343 - unsigned int oldflags, mask, i;
344 - unsigned int jflag;
347 if (!inode_owner_or_capable(inode))
349 @@ -231,89 +473,8 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
351 flags = ext4_mask_flags(inode->i_mode, flags);
354 mutex_lock(&inode->i_mutex);
355 - /* Is it quota file? Do not allow user to mess with it */
356 - if (IS_NOQUOTA(inode))
359 - oldflags = ei->i_flags;
361 - /* The JOURNAL_DATA flag is modifiable only by root */
362 - jflag = flags & EXT4_JOURNAL_DATA_FL;
365 - * The IMMUTABLE and APPEND_ONLY flags can only be changed by
366 - * the relevant capability.
368 - * This test looks nicer. Thanks to Pauline Middelink
370 - if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
371 - if (!capable(CAP_LINUX_IMMUTABLE))
376 - * The JOURNAL_DATA flag can only be changed by
377 - * the relevant capability.
379 - if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
380 - if (!capable(CAP_SYS_RESOURCE))
383 - if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
386 - if (flags & EXT4_EOFBLOCKS_FL) {
387 - /* we don't support adding EOFBLOCKS flag */
388 - if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
392 - } else if (oldflags & EXT4_EOFBLOCKS_FL)
393 - ext4_truncate(inode);
395 - handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
396 - if (IS_ERR(handle)) {
397 - err = PTR_ERR(handle);
400 - if (IS_SYNC(inode))
401 - ext4_handle_sync(handle);
402 - err = ext4_reserve_inode_write(handle, inode, &iloc);
406 - for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
407 - if (!(mask & EXT4_FL_USER_MODIFIABLE))
410 - ext4_set_inode_flag(inode, i);
412 - ext4_clear_inode_flag(inode, i);
415 - ext4_set_inode_flags(inode);
416 - inode->i_ctime = ext4_current_time(inode);
418 - err = ext4_mark_iloc_dirty(handle, inode, &iloc);
420 - ext4_journal_stop(handle);
424 - if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
425 - err = ext4_change_inode_journal_flag(inode, jflag);
429 - if (flags & EXT4_EXTENTS_FL)
430 - err = ext4_ext_migrate(inode);
432 - err = ext4_ind_migrate(inode);
436 + err = ext4_ioctl_setflags(inode, flags);
437 mutex_unlock(&inode->i_mutex);
438 mnt_drop_write_file(filp);
440 @@ -617,6 +778,62 @@ resizefs_out:
442 case EXT4_IOC_PRECACHE_EXTENTS:
443 return ext4_ext_precache(inode);
444 + case EXT4_IOC_FSGETXATTR:
447 + unsigned int flags;
449 + memset(&fa, 0, sizeof(struct fsxattr));
450 + ext4_get_inode_flags(ei);
451 + flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
452 + fa.fsx_xflags = ext4_iflags_to_xflags(flags);
454 + if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
455 + EXT4_FEATURE_RO_COMPAT_PROJECT)) {
456 + fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
457 + EXT4_I(inode)->i_projid);
460 + if (copy_to_user((struct fsxattr __user *)arg,
465 + case EXT4_IOC_FSSETXATTR:
470 + if (copy_from_user(&fa, (struct fsxattr __user *)arg,
474 + /* Make sure caller has proper permission */
475 + if (!inode_owner_or_capable(inode))
478 + err = mnt_want_write_file(filp);
482 + flags = ext4_xflags_to_iflags(fa.fsx_xflags);
483 + flags = ext4_mask_flags(inode->i_mode, flags);
485 + mutex_lock(&inode->i_mutex);
486 + flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
487 + (flags & EXT4_FL_XFLAG_VISIBLE);
488 + err = ext4_ioctl_setflags(inode, flags);
489 + mutex_unlock(&inode->i_mutex);
490 + mnt_drop_write_file(filp);
494 + err = ext4_ioctl_setproject(filp, fa.fsx_projid);