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