Whamcloud - gitweb
LU-10897 kernel: kernel upgrade RHEL7.5 [3.10.0-862.2.3.el7]
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel7.5 / ext4-projid-xfs-ioctls.patch
1 Index: linux-stage/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/ext4.h
4 +++ linux-stage/fs/ext4/ext4.h
5 @@ -397,6 +397,13 @@ struct flex_groups {
6  #define EXT4_FL_USER_VISIBLE           0x304BDFFF /* User visible flags */
7  #define EXT4_FL_USER_MODIFIABLE                0x204380FF /* User modifiable flags */
8  
9 +#define EXT4_FL_XFLAG_VISIBLE          (EXT4_SYNC_FL | \
10 +                                        EXT4_IMMUTABLE_FL | \
11 +                                        EXT4_APPEND_FL | \
12 +                                        EXT4_NODUMP_FL | \
13 +                                        EXT4_NOATIME_FL | \
14 +                                        EXT4_PROJINHERIT_FL)
15 +
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 @@ -622,6 +629,44 @@ enum {
20  #define EXT4_IOC_SWAP_BOOT             _IO('f', 17)
21  #define EXT4_IOC_PRECACHE_EXTENTS      _IO('f', 18)
22  
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)
27 +/*
28 + * Structure for FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR.
29 + */
30 +struct fsxattr {
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];
36 +};
37 +
38 +/*
39 + * Flags for the fsx_xflags field
40 + */
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) */
57 +
58 +#define EXT4_IOC_FSGETXATTR            FS_IOC_FSGETXATTR
59 +#define EXT4_IOC_FSSETXATTR            FS_IOC_FSSETXATTR
60 +
61  #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
62  /*
63   * ioctl commands in 32 bit emulation
64 @@ -2405,6 +2450,7 @@ extern int ext4_ind_remove_space(handle_
65  /* ioctl.c */
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);
69  
70  /* migrate.c */
71  extern int ext4_ext_migrate(struct inode *);
72 Index: linux-stage/fs/ext4/ioctl.c
73 ===================================================================
74 --- linux-stage.orig/fs/ext4/ioctl.c
75 +++ linux-stage/fs/ext4/ioctl.c
76 @@ -16,6 +16,7 @@
77  #include <linux/file.h>
78  #include <linux/uuid.h>
79  #include <asm/uaccess.h>
80 +#include <linux/quotaops.h>
81  #include "ext4_jbd2.h"
82  #include "ext4.h"
83  
84 @@ -199,6 +200,260 @@ journal_err_out:
85         return err;
86  }
87  
88 +static int ext4_ioctl_setflags(struct inode *inode,
89 +                              unsigned int flags)
90 +{
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;
96 +       unsigned int jflag;
97 +
98 +       /* Is it quota file? Do not allow user to mess with it */
99 +       if (IS_NOQUOTA(inode))
100 +               goto flags_out;
101 +
102 +       oldflags = ei->i_flags;
103 +
104 +       /* The JOURNAL_DATA flag is modifiable only by root */
105 +       jflag = flags & EXT4_JOURNAL_DATA_FL;
106 +
107 +       /*
108 +        * The IMMUTABLE and APPEND_ONLY flags can only be changed by
109 +        * the relevant capability.
110 +        *
111 +        * This test looks nicer. Thanks to Pauline Middelink
112 +        */
113 +       if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
114 +               if (!capable(CAP_LINUX_IMMUTABLE))
115 +                       goto flags_out;
116 +       }
117 +
118 +       /*
119 +        * The JOURNAL_DATA flag can only be changed by
120 +        * the relevant capability.
121 +        */
122 +       if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
123 +               if (!capable(CAP_SYS_RESOURCE))
124 +                       goto flags_out;
125 +       }
126 +       if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
127 +               migrate = 1;
128 +
129 +       if (flags & EXT4_EOFBLOCKS_FL) {
130 +               /* we don't support adding EOFBLOCKS flag */
131 +               if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
132 +                       err = -EOPNOTSUPP;
133 +                       goto flags_out;
134 +               }
135 +       } else if (oldflags & EXT4_EOFBLOCKS_FL)
136 +               ext4_truncate(inode);
137 +
138 +       handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
139 +       if (IS_ERR(handle)) {
140 +               err = PTR_ERR(handle);
141 +               goto flags_out;
142 +       }
143 +       if (IS_SYNC(inode))
144 +               ext4_handle_sync(handle);
145 +       err = ext4_reserve_inode_write(handle, inode, &iloc);
146 +       if (err)
147 +               goto flags_err;
148 +
149 +       for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
150 +               if (!(mask & EXT4_FL_USER_MODIFIABLE))
151 +                       continue;
152 +               if (mask & flags)
153 +                       ext4_set_inode_flag(inode, i);
154 +               else
155 +                       ext4_clear_inode_flag(inode, i);
156 +       }
157 +
158 +       ext4_set_inode_flags(inode);
159 +       inode->i_ctime = ext4_current_time(inode);
160 +
161 +       err = ext4_mark_iloc_dirty(handle, inode, &iloc);
162 +flags_err:
163 +       ext4_journal_stop(handle);
164 +       if (err)
165 +               goto flags_out;
166 +
167 +       if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
168 +               /*
169 +                * Changes to the journaling mode can cause unsafe
170 +                * changes to S_DAX if we are using the DAX mount option.
171 +                */
172 +               if (test_opt(inode->i_sb, DAX)) {
173 +                       err = -EBUSY;
174 +                       goto flags_out;
175 +               }
176 +               err = ext4_change_inode_journal_flag(inode, jflag);
177 +       }
178 +       if (err)
179 +               goto flags_out;
180 +       if (migrate) {
181 +               if (flags & EXT4_EXTENTS_FL)
182 +                       err = ext4_ext_migrate(inode);
183 +               else
184 +                       err = ext4_ind_migrate(inode);
185 +       }
186 +
187 +flags_out:
188 +       return err;
189 +}
190 +
191 +#ifdef CONFIG_QUOTA
192 +int __ext4_ioctl_setproject(struct inode *inode, __u32 projid)
193 +{
194 +       struct super_block *sb = inode->i_sb;
195 +       struct ext4_inode_info *ei = EXT4_I(inode);
196 +       int err, rc;
197 +       handle_t *handle;
198 +       kprojid_t kprojid;
199 +       struct ext4_iloc iloc;
200 +       struct ext4_inode *raw_inode;
201 +       struct dquot *transfer_to[EXT4_MAXQUOTAS] = { };
202 +
203 +       if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
204 +                       EXT4_FEATURE_RO_COMPAT_PROJECT)) {
205 +               BUG_ON(__kprojid_val(EXT4_I(inode)->i_projid)
206 +                      != EXT4_DEF_PROJID);
207 +               if (projid != EXT4_DEF_PROJID)
208 +                       return -EOPNOTSUPP;
209 +               else
210 +                       return 0;
211 +       }
212 +
213 +       if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
214 +               return -EOPNOTSUPP;
215 +
216 +       kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
217 +
218 +       if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
219 +               return 0;
220 +
221 +       err = -EPERM;
222 +       mutex_lock(&inode->i_mutex);
223 +       /* Is it quota file? Do not allow user to mess with it */
224 +       if (IS_NOQUOTA(inode))
225 +               goto out_unlock;
226 +
227 +       err = ext4_get_inode_loc(inode, &iloc);
228 +       if (err)
229 +               goto out_unlock;
230 +
231 +       raw_inode = ext4_raw_inode(&iloc);
232 +       if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
233 +               err = -EOVERFLOW;
234 +               brelse(iloc.bh);
235 +               goto out_unlock;
236 +       }
237 +       brelse(iloc.bh);
238 +
239 +       dquot_initialize(inode);
240 +
241 +       handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
242 +               EXT4_QUOTA_INIT_BLOCKS(sb) +
243 +               EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
244 +       if (IS_ERR(handle)) {
245 +               err = PTR_ERR(handle);
246 +               goto out_unlock;
247 +       }
248 +
249 +       err = ext4_reserve_inode_write(handle, inode, &iloc);
250 +       if (err)
251 +               goto out_stop;
252 +
253 +       transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
254 +       if (transfer_to[PRJQUOTA]) {
255 +               err = __dquot_transfer(inode, transfer_to);
256 +               dqput(transfer_to[PRJQUOTA]);
257 +               if (err)
258 +                       goto out_dirty;
259 +       }
260 +
261 +       EXT4_I(inode)->i_projid = kprojid;
262 +       inode->i_ctime = ext4_current_time(inode);
263 +out_dirty:
264 +       rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
265 +       if (!err)
266 +               err = rc;
267 +out_stop:
268 +       ext4_journal_stop(handle);
269 +out_unlock:
270 +       mutex_unlock(&inode->i_mutex);
271 +       return err;
272 +
273 +
274 +}
275 +EXPORT_SYMBOL(__ext4_ioctl_setproject);
276 +
277 +static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
278 +{
279 +       int err;
280 +       struct inode *inode = file_inode(filp);
281 +
282 +       err = mnt_want_write_file(filp);
283 +       if (err)
284 +               return err;
285 +
286 +       err = __ext4_ioctl_setproject(inode, projid);
287 +
288 +       mnt_drop_write_file(filp);
289 +       return err;
290 +}
291 +#else
292 +static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
293 +{
294 +       if (projid != EXT4_DEF_PROJID)
295 +               return -EOPNOTSUPP;
296 +       return 0;
297 +}
298 +#endif
299 +
300 +
301 +/* Transfer internal flags to xflags */
302 +static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
303 +{
304 +       __u32 xflags = 0;
305 +
306 +       if (iflags & EXT4_SYNC_FL)
307 +               xflags |= FS_XFLAG_SYNC;
308 +       if (iflags & EXT4_IMMUTABLE_FL)
309 +               xflags |= FS_XFLAG_IMMUTABLE;
310 +       if (iflags & EXT4_APPEND_FL)
311 +               xflags |= FS_XFLAG_APPEND;
312 +       if (iflags & EXT4_NODUMP_FL)
313 +               xflags |= FS_XFLAG_NODUMP;
314 +       if (iflags & EXT4_NOATIME_FL)
315 +               xflags |= FS_XFLAG_NOATIME;
316 +       if (iflags & EXT4_PROJINHERIT_FL)
317 +               xflags |= FS_XFLAG_PROJINHERIT;
318 +       return xflags;
319 +}
320 +
321 +/* Transfer xflags flags to internal */
322 +static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
323 +{
324 +       unsigned long iflags = 0;
325 +
326 +       if (xflags & FS_XFLAG_SYNC)
327 +               iflags |= EXT4_SYNC_FL;
328 +       if (xflags & FS_XFLAG_IMMUTABLE)
329 +               iflags |= EXT4_IMMUTABLE_FL;
330 +       if (xflags & FS_XFLAG_APPEND)
331 +               iflags |= EXT4_APPEND_FL;
332 +       if (xflags & FS_XFLAG_NODUMP)
333 +               iflags |= EXT4_NODUMP_FL;
334 +       if (xflags & FS_XFLAG_NOATIME)
335 +               iflags |= EXT4_NOATIME_FL;
336 +       if (xflags & FS_XFLAG_PROJINHERIT)
337 +               iflags |= EXT4_PROJINHERIT_FL;
338 +
339 +       return iflags;
340 +}
341 +
342  long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
343  {
344         struct inode *inode = file_inode(filp);
345 @@ -214,11 +469,7 @@ long ext4_ioctl(struct file *filp, unsig
346                 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
347                 return put_user(flags, (int __user *) arg);
348         case EXT4_IOC_SETFLAGS: {
349 -               handle_t *handle = NULL;
350 -               int err, migrate = 0;
351 -               struct ext4_iloc iloc;
352 -               unsigned int oldflags, mask, i;
353 -               unsigned int jflag;
354 +               int err;
355  
356                 if (!inode_owner_or_capable(inode))
357                         return -EACCES;
358 @@ -232,98 +483,8 @@ long ext4_ioctl(struct file *filp, unsig
359  
360                 flags = ext4_mask_flags(inode->i_mode, flags);
361  
362 -               err = -EPERM;
363                 mutex_lock(&inode->i_mutex);
364 -               /* Is it quota file? Do not allow user to mess with it */
365 -               if (IS_NOQUOTA(inode))
366 -                       goto flags_out;
367 -
368 -               oldflags = ei->i_flags;
369 -
370 -               /* The JOURNAL_DATA flag is modifiable only by root */
371 -               jflag = flags & EXT4_JOURNAL_DATA_FL;
372 -
373 -               /*
374 -                * The IMMUTABLE and APPEND_ONLY flags can only be changed by
375 -                * the relevant capability.
376 -                *
377 -                * This test looks nicer. Thanks to Pauline Middelink
378 -                */
379 -               if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
380 -                       if (!capable(CAP_LINUX_IMMUTABLE))
381 -                               goto flags_out;
382 -               }
383 -
384 -               /*
385 -                * The JOURNAL_DATA flag can only be changed by
386 -                * the relevant capability.
387 -                */
388 -               if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
389 -                       if (!capable(CAP_SYS_RESOURCE))
390 -                               goto flags_out;
391 -               }
392 -               if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
393 -                       migrate = 1;
394 -
395 -               if (flags & EXT4_EOFBLOCKS_FL) {
396 -                       /* we don't support adding EOFBLOCKS flag */
397 -                       if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
398 -                               err = -EOPNOTSUPP;
399 -                               goto flags_out;
400 -                       }
401 -               } else if (oldflags & EXT4_EOFBLOCKS_FL)
402 -                       ext4_truncate(inode);
403 -
404 -               handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
405 -               if (IS_ERR(handle)) {
406 -                       err = PTR_ERR(handle);
407 -                       goto flags_out;
408 -               }
409 -               if (IS_SYNC(inode))
410 -                       ext4_handle_sync(handle);
411 -               err = ext4_reserve_inode_write(handle, inode, &iloc);
412 -               if (err)
413 -                       goto flags_err;
414 -
415 -               for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
416 -                       if (!(mask & EXT4_FL_USER_MODIFIABLE))
417 -                               continue;
418 -                       if (mask & flags)
419 -                               ext4_set_inode_flag(inode, i);
420 -                       else
421 -                               ext4_clear_inode_flag(inode, i);
422 -               }
423 -
424 -               ext4_set_inode_flags(inode);
425 -               inode->i_ctime = ext4_current_time(inode);
426 -
427 -               err = ext4_mark_iloc_dirty(handle, inode, &iloc);
428 -flags_err:
429 -               ext4_journal_stop(handle);
430 -               if (err)
431 -                       goto flags_out;
432 -
433 -               if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
434 -                       /*
435 -                        * Changes to the journaling mode can cause unsafe
436 -                        * changes to S_DAX if we are using the DAX mount option.
437 -                        */
438 -                       if (test_opt(inode->i_sb, DAX)) {
439 -                               err = -EBUSY;
440 -                               goto flags_out;
441 -                       }
442 -                       err = ext4_change_inode_journal_flag(inode, jflag);
443 -                       if (err)
444 -                               goto flags_out;
445 -               }
446 -               if (migrate) {
447 -                       if (flags & EXT4_EXTENTS_FL)
448 -                               err = ext4_ext_migrate(inode);
449 -                       else
450 -                               err = ext4_ind_migrate(inode);
451 -               }
452 -
453 -flags_out:
454 +               err = ext4_ioctl_setflags(inode, flags);
455                 mutex_unlock(&inode->i_mutex);
456                 mnt_drop_write_file(filp);
457                 return err;
458 @@ -632,6 +793,62 @@ resizefs_out:
459         }
460         case EXT4_IOC_PRECACHE_EXTENTS:
461                 return ext4_ext_precache(inode);
462 +       case EXT4_IOC_FSGETXATTR:
463 +       {
464 +               struct fsxattr fa;
465 +               unsigned int flags;
466 +
467 +               memset(&fa, 0, sizeof(struct fsxattr));
468 +               ext4_get_inode_flags(ei);
469 +               flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
470 +               fa.fsx_xflags = ext4_iflags_to_xflags(flags);
471 +
472 +               if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
473 +                               EXT4_FEATURE_RO_COMPAT_PROJECT)) {
474 +                       fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
475 +                               EXT4_I(inode)->i_projid);
476 +               }
477 +
478 +               if (copy_to_user((struct fsxattr __user *)arg,
479 +                                &fa, sizeof(fa)))
480 +                       return -EFAULT;
481 +               return 0;
482 +       }
483 +       case EXT4_IOC_FSSETXATTR:
484 +       {
485 +               struct fsxattr fa;
486 +               int err;
487 +
488 +               if (copy_from_user(&fa, (struct fsxattr __user *)arg,
489 +                                  sizeof(fa)))
490 +                       return -EFAULT;
491 +
492 +               /* Make sure caller has proper permission */
493 +               if (!inode_owner_or_capable(inode))
494 +                       return -EACCES;
495 +
496 +               err = mnt_want_write_file(filp);
497 +               if (err)
498 +                       return err;
499 +
500 +               flags = ext4_xflags_to_iflags(fa.fsx_xflags);
501 +               flags = ext4_mask_flags(inode->i_mode, flags);
502 +
503 +               mutex_lock(&inode->i_mutex);
504 +               flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
505 +                        (flags & EXT4_FL_XFLAG_VISIBLE);
506 +               err = ext4_ioctl_setflags(inode, flags);
507 +               mutex_unlock(&inode->i_mutex);
508 +               mnt_drop_write_file(filp);
509 +               if (err)
510 +                       return err;
511 +
512 +               err = ext4_ioctl_setproject(filp, fa.fsx_projid);
513 +               if (err)
514 +                       return err;
515 +
516 +               return 0;
517 +       }
518  
519         default:
520                 return -ENOTTY;