]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/xfs/xfs_iops.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_iops.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_inode.h"
29 #include "xfs_bmap.h"
30 #include "xfs_bmap_util.h"
31 #include "xfs_acl.h"
32 #include "xfs_quota.h"
33 #include "xfs_error.h"
34 #include "xfs_attr.h"
35 #include "xfs_trans.h"
36 #include "xfs_trace.h"
37 #include "xfs_icache.h"
38 #include "xfs_symlink.h"
39 #include "xfs_da_btree.h"
40 #include "xfs_dir2_priv.h"
41 #include "xfs_dinode.h"
42 #include "xfs_trans_space.h"
43
44 #include <linux/capability.h>
45 #include <linux/xattr.h>
46 #include <linux/namei.h>
47 #include <linux/posix_acl.h>
48 #include <linux/security.h>
49 #include <linux/fiemap.h>
50 #include <linux/slab.h>
51
52 /*
53 * Directories have different lock order w.r.t. mmap_sem compared to regular
54 * files. This is due to readdir potentially triggering page faults on a user
55 * buffer inside filldir(), and this happens with the ilock on the directory
56 * held. For regular files, the lock order is the other way around - the
57 * mmap_sem is taken during the page fault, and then we lock the ilock to do
58 * block mapping. Hence we need a different class for the directory ilock so
59 * that lockdep can tell them apart.
60 */
61 static struct lock_class_key xfs_nondir_ilock_class;
62 static struct lock_class_key xfs_dir_ilock_class;
63
64 static int
65 xfs_initxattrs(
66 struct inode *inode,
67 const struct xattr *xattr_array,
68 void *fs_info)
69 {
70 const struct xattr *xattr;
71 struct xfs_inode *ip = XFS_I(inode);
72 int error = 0;
73
74 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
75 error = xfs_attr_set(ip, xattr->name, xattr->value,
76 xattr->value_len, ATTR_SECURE);
77 if (error < 0)
78 break;
79 }
80 return error;
81 }
82
83 /*
84 * Hook in SELinux. This is not quite correct yet, what we really need
85 * here (as we do for default ACLs) is a mechanism by which creation of
86 * these attrs can be journalled at inode creation time (along with the
87 * inode, of course, such that log replay can't cause these to be lost).
88 */
89
90 STATIC int
91 xfs_init_security(
92 struct inode *inode,
93 struct inode *dir,
94 const struct qstr *qstr)
95 {
96 return security_inode_init_security(inode, dir, qstr,
97 &xfs_initxattrs, NULL);
98 }
99
100 static void
101 xfs_dentry_to_name(
102 struct xfs_name *namep,
103 struct dentry *dentry,
104 int mode)
105 {
106 namep->name = dentry->d_name.name;
107 namep->len = dentry->d_name.len;
108 namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
109 }
110
111 STATIC void
112 xfs_cleanup_inode(
113 struct inode *dir,
114 struct inode *inode,
115 struct dentry *dentry)
116 {
117 struct xfs_name teardown;
118
119 /* Oh, the horror.
120 * If we can't add the ACL or we fail in
121 * xfs_init_security we must back out.
122 * ENOSPC can hit here, among other things.
123 */
124 xfs_dentry_to_name(&teardown, dentry, 0);
125
126 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
127 iput(inode);
128 }
129
130 STATIC int
131 xfs_vn_mknod(
132 struct inode *dir,
133 struct dentry *dentry,
134 umode_t mode,
135 dev_t rdev)
136 {
137 struct inode *inode;
138 struct xfs_inode *ip = NULL;
139 struct posix_acl *default_acl, *acl;
140 struct xfs_name name;
141 int error;
142
143 /*
144 * Irix uses Missed'em'V split, but doesn't want to see
145 * the upper 5 bits of (14bit) major.
146 */
147 if (S_ISCHR(mode) || S_ISBLK(mode)) {
148 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
149 return -EINVAL;
150 rdev = sysv_encode_dev(rdev);
151 } else {
152 rdev = 0;
153 }
154
155 error = posix_acl_create(dir, &mode, &default_acl, &acl);
156 if (error)
157 return error;
158
159 xfs_dentry_to_name(&name, dentry, mode);
160 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
161 if (unlikely(error))
162 goto out_free_acl;
163
164 inode = VFS_I(ip);
165
166 error = xfs_init_security(inode, dir, &dentry->d_name);
167 if (unlikely(error))
168 goto out_cleanup_inode;
169
170 #ifdef CONFIG_XFS_POSIX_ACL
171 if (default_acl) {
172 error = xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
173 if (error)
174 goto out_cleanup_inode;
175 }
176 if (acl) {
177 error = xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
178 if (error)
179 goto out_cleanup_inode;
180 }
181 #endif
182
183 d_instantiate(dentry, inode);
184 out_free_acl:
185 if (default_acl)
186 posix_acl_release(default_acl);
187 if (acl)
188 posix_acl_release(acl);
189 return -error;
190
191 out_cleanup_inode:
192 xfs_cleanup_inode(dir, inode, dentry);
193 goto out_free_acl;
194 }
195
196 STATIC int
197 xfs_vn_create(
198 struct inode *dir,
199 struct dentry *dentry,
200 umode_t mode,
201 bool flags)
202 {
203 return xfs_vn_mknod(dir, dentry, mode, 0);
204 }
205
206 STATIC int
207 xfs_vn_mkdir(
208 struct inode *dir,
209 struct dentry *dentry,
210 umode_t mode)
211 {
212 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
213 }
214
215 STATIC struct dentry *
216 xfs_vn_lookup(
217 struct inode *dir,
218 struct dentry *dentry,
219 unsigned int flags)
220 {
221 struct xfs_inode *cip;
222 struct xfs_name name;
223 int error;
224
225 if (dentry->d_name.len >= MAXNAMELEN)
226 return ERR_PTR(-ENAMETOOLONG);
227
228 xfs_dentry_to_name(&name, dentry, 0);
229 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
230 if (unlikely(error)) {
231 if (unlikely(error != ENOENT))
232 return ERR_PTR(-error);
233 d_add(dentry, NULL);
234 return NULL;
235 }
236
237 return d_splice_alias(VFS_I(cip), dentry);
238 }
239
240 STATIC struct dentry *
241 xfs_vn_ci_lookup(
242 struct inode *dir,
243 struct dentry *dentry,
244 unsigned int flags)
245 {
246 struct xfs_inode *ip;
247 struct xfs_name xname;
248 struct xfs_name ci_name;
249 struct qstr dname;
250 int error;
251
252 if (dentry->d_name.len >= MAXNAMELEN)
253 return ERR_PTR(-ENAMETOOLONG);
254
255 xfs_dentry_to_name(&xname, dentry, 0);
256 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
257 if (unlikely(error)) {
258 if (unlikely(error != ENOENT))
259 return ERR_PTR(-error);
260 /*
261 * call d_add(dentry, NULL) here when d_drop_negative_children
262 * is called in xfs_vn_mknod (ie. allow negative dentries
263 * with CI filesystems).
264 */
265 return NULL;
266 }
267
268 /* if exact match, just splice and exit */
269 if (!ci_name.name)
270 return d_splice_alias(VFS_I(ip), dentry);
271
272 /* else case-insensitive match... */
273 dname.name = ci_name.name;
274 dname.len = ci_name.len;
275 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
276 kmem_free(ci_name.name);
277 return dentry;
278 }
279
280 STATIC int
281 xfs_vn_link(
282 struct dentry *old_dentry,
283 struct inode *dir,
284 struct dentry *dentry)
285 {
286 struct inode *inode = old_dentry->d_inode;
287 struct xfs_name name;
288 int error;
289
290 xfs_dentry_to_name(&name, dentry, inode->i_mode);
291
292 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
293 if (unlikely(error))
294 return -error;
295
296 ihold(inode);
297 d_instantiate(dentry, inode);
298 return 0;
299 }
300
301 STATIC int
302 xfs_vn_unlink(
303 struct inode *dir,
304 struct dentry *dentry)
305 {
306 struct xfs_name name;
307 int error;
308
309 xfs_dentry_to_name(&name, dentry, 0);
310
311 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
312 if (error)
313 return error;
314
315 /*
316 * With unlink, the VFS makes the dentry "negative": no inode,
317 * but still hashed. This is incompatible with case-insensitive
318 * mode, so invalidate (unhash) the dentry in CI-mode.
319 */
320 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
321 d_invalidate(dentry);
322 return 0;
323 }
324
325 STATIC int
326 xfs_vn_symlink(
327 struct inode *dir,
328 struct dentry *dentry,
329 const char *symname)
330 {
331 struct inode *inode;
332 struct xfs_inode *cip = NULL;
333 struct xfs_name name;
334 int error;
335 umode_t mode;
336
337 mode = S_IFLNK |
338 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
339 xfs_dentry_to_name(&name, dentry, mode);
340
341 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
342 if (unlikely(error))
343 goto out;
344
345 inode = VFS_I(cip);
346
347 error = xfs_init_security(inode, dir, &dentry->d_name);
348 if (unlikely(error))
349 goto out_cleanup_inode;
350
351 d_instantiate(dentry, inode);
352 return 0;
353
354 out_cleanup_inode:
355 xfs_cleanup_inode(dir, inode, dentry);
356 out:
357 return -error;
358 }
359
360 STATIC int
361 xfs_vn_rename(
362 struct inode *odir,
363 struct dentry *odentry,
364 struct inode *ndir,
365 struct dentry *ndentry)
366 {
367 struct inode *new_inode = ndentry->d_inode;
368 struct xfs_name oname;
369 struct xfs_name nname;
370
371 xfs_dentry_to_name(&oname, odentry, 0);
372 xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
373
374 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
375 XFS_I(ndir), &nname, new_inode ?
376 XFS_I(new_inode) : NULL);
377 }
378
379 /*
380 * careful here - this function can get called recursively, so
381 * we need to be very careful about how much stack we use.
382 * uio is kmalloced for this reason...
383 */
384 STATIC void *
385 xfs_vn_follow_link(
386 struct dentry *dentry,
387 struct nameidata *nd)
388 {
389 char *link;
390 int error = -ENOMEM;
391
392 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
393 if (!link)
394 goto out_err;
395
396 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
397 if (unlikely(error))
398 goto out_kfree;
399
400 nd_set_link(nd, link);
401 return NULL;
402
403 out_kfree:
404 kfree(link);
405 out_err:
406 nd_set_link(nd, ERR_PTR(error));
407 return NULL;
408 }
409
410 STATIC int
411 xfs_vn_getattr(
412 struct vfsmount *mnt,
413 struct dentry *dentry,
414 struct kstat *stat)
415 {
416 struct inode *inode = dentry->d_inode;
417 struct xfs_inode *ip = XFS_I(inode);
418 struct xfs_mount *mp = ip->i_mount;
419
420 trace_xfs_getattr(ip);
421
422 if (XFS_FORCED_SHUTDOWN(mp))
423 return -XFS_ERROR(EIO);
424
425 stat->size = XFS_ISIZE(ip);
426 stat->dev = inode->i_sb->s_dev;
427 stat->mode = ip->i_d.di_mode;
428 stat->nlink = ip->i_d.di_nlink;
429 stat->uid = inode->i_uid;
430 stat->gid = inode->i_gid;
431 stat->ino = ip->i_ino;
432 stat->atime = inode->i_atime;
433 stat->mtime = inode->i_mtime;
434 stat->ctime = inode->i_ctime;
435 stat->blocks =
436 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
437
438
439 switch (inode->i_mode & S_IFMT) {
440 case S_IFBLK:
441 case S_IFCHR:
442 stat->blksize = BLKDEV_IOSIZE;
443 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
444 sysv_minor(ip->i_df.if_u2.if_rdev));
445 break;
446 default:
447 if (XFS_IS_REALTIME_INODE(ip)) {
448 /*
449 * If the file blocks are being allocated from a
450 * realtime volume, then return the inode's realtime
451 * extent size or the realtime volume's extent size.
452 */
453 stat->blksize =
454 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
455 } else
456 stat->blksize = xfs_preferred_iosize(mp);
457 stat->rdev = 0;
458 break;
459 }
460
461 return 0;
462 }
463
464 static void
465 xfs_setattr_mode(
466 struct xfs_inode *ip,
467 struct iattr *iattr)
468 {
469 struct inode *inode = VFS_I(ip);
470 umode_t mode = iattr->ia_mode;
471
472 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
473
474 ip->i_d.di_mode &= S_IFMT;
475 ip->i_d.di_mode |= mode & ~S_IFMT;
476
477 inode->i_mode &= S_IFMT;
478 inode->i_mode |= mode & ~S_IFMT;
479 }
480
481 static void
482 xfs_setattr_time(
483 struct xfs_inode *ip,
484 struct iattr *iattr)
485 {
486 struct inode *inode = VFS_I(ip);
487
488 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
489
490 if (iattr->ia_valid & ATTR_ATIME) {
491 inode->i_atime = iattr->ia_atime;
492 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
493 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
494 }
495 if (iattr->ia_valid & ATTR_CTIME) {
496 inode->i_ctime = iattr->ia_ctime;
497 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
498 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
499 }
500 if (iattr->ia_valid & ATTR_MTIME) {
501 inode->i_mtime = iattr->ia_mtime;
502 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
503 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
504 }
505 }
506
507 int
508 xfs_setattr_nonsize(
509 struct xfs_inode *ip,
510 struct iattr *iattr,
511 int flags)
512 {
513 xfs_mount_t *mp = ip->i_mount;
514 struct inode *inode = VFS_I(ip);
515 int mask = iattr->ia_valid;
516 xfs_trans_t *tp;
517 int error;
518 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
519 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
520 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
521 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
522
523 trace_xfs_setattr(ip);
524
525 /* If acls are being inherited, we already have this checked */
526 if (!(flags & XFS_ATTR_NOACL)) {
527 if (mp->m_flags & XFS_MOUNT_RDONLY)
528 return XFS_ERROR(EROFS);
529
530 if (XFS_FORCED_SHUTDOWN(mp))
531 return XFS_ERROR(EIO);
532
533 error = -inode_change_ok(inode, iattr);
534 if (error)
535 return XFS_ERROR(error);
536 }
537
538 ASSERT((mask & ATTR_SIZE) == 0);
539
540 /*
541 * If disk quotas is on, we make sure that the dquots do exist on disk,
542 * before we start any other transactions. Trying to do this later
543 * is messy. We don't care to take a readlock to look at the ids
544 * in inode here, because we can't hold it across the trans_reserve.
545 * If the IDs do change before we take the ilock, we're covered
546 * because the i_*dquot fields will get updated anyway.
547 */
548 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
549 uint qflags = 0;
550
551 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
552 uid = iattr->ia_uid;
553 qflags |= XFS_QMOPT_UQUOTA;
554 } else {
555 uid = inode->i_uid;
556 }
557 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
558 gid = iattr->ia_gid;
559 qflags |= XFS_QMOPT_GQUOTA;
560 } else {
561 gid = inode->i_gid;
562 }
563
564 /*
565 * We take a reference when we initialize udqp and gdqp,
566 * so it is important that we never blindly double trip on
567 * the same variable. See xfs_create() for an example.
568 */
569 ASSERT(udqp == NULL);
570 ASSERT(gdqp == NULL);
571 error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
572 xfs_kgid_to_gid(gid),
573 xfs_get_projid(ip),
574 qflags, &udqp, &gdqp, NULL);
575 if (error)
576 return error;
577 }
578
579 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
580 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
581 if (error)
582 goto out_dqrele;
583
584 xfs_ilock(ip, XFS_ILOCK_EXCL);
585
586 /*
587 * Change file ownership. Must be the owner or privileged.
588 */
589 if (mask & (ATTR_UID|ATTR_GID)) {
590 /*
591 * These IDs could have changed since we last looked at them.
592 * But, we're assured that if the ownership did change
593 * while we didn't have the inode locked, inode's dquot(s)
594 * would have changed also.
595 */
596 iuid = inode->i_uid;
597 igid = inode->i_gid;
598 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
599 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
600
601 /*
602 * Do a quota reservation only if uid/gid is actually
603 * going to change.
604 */
605 if (XFS_IS_QUOTA_RUNNING(mp) &&
606 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
607 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
608 ASSERT(tp);
609 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
610 NULL, capable(CAP_FOWNER) ?
611 XFS_QMOPT_FORCE_RES : 0);
612 if (error) /* out of quota */
613 goto out_trans_cancel;
614 }
615 }
616
617 xfs_trans_ijoin(tp, ip, 0);
618
619 /*
620 * Change file ownership. Must be the owner or privileged.
621 */
622 if (mask & (ATTR_UID|ATTR_GID)) {
623 /*
624 * CAP_FSETID overrides the following restrictions:
625 *
626 * The set-user-ID and set-group-ID bits of a file will be
627 * cleared upon successful return from chown()
628 */
629 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
630 !capable(CAP_FSETID))
631 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
632
633 /*
634 * Change the ownerships and register quota modifications
635 * in the transaction.
636 */
637 if (!uid_eq(iuid, uid)) {
638 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
639 ASSERT(mask & ATTR_UID);
640 ASSERT(udqp);
641 olddquot1 = xfs_qm_vop_chown(tp, ip,
642 &ip->i_udquot, udqp);
643 }
644 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
645 inode->i_uid = uid;
646 }
647 if (!gid_eq(igid, gid)) {
648 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
649 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
650 !XFS_IS_PQUOTA_ON(mp));
651 ASSERT(mask & ATTR_GID);
652 ASSERT(gdqp);
653 olddquot2 = xfs_qm_vop_chown(tp, ip,
654 &ip->i_gdquot, gdqp);
655 }
656 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
657 inode->i_gid = gid;
658 }
659 }
660
661 if (mask & ATTR_MODE)
662 xfs_setattr_mode(ip, iattr);
663 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
664 xfs_setattr_time(ip, iattr);
665
666 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
667
668 XFS_STATS_INC(xs_ig_attrchg);
669
670 if (mp->m_flags & XFS_MOUNT_WSYNC)
671 xfs_trans_set_sync(tp);
672 error = xfs_trans_commit(tp, 0);
673
674 xfs_iunlock(ip, XFS_ILOCK_EXCL);
675
676 /*
677 * Release any dquot(s) the inode had kept before chown.
678 */
679 xfs_qm_dqrele(olddquot1);
680 xfs_qm_dqrele(olddquot2);
681 xfs_qm_dqrele(udqp);
682 xfs_qm_dqrele(gdqp);
683
684 if (error)
685 return XFS_ERROR(error);
686
687 /*
688 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
689 * update. We could avoid this with linked transactions
690 * and passing down the transaction pointer all the way
691 * to attr_set. No previous user of the generic
692 * Posix ACL code seems to care about this issue either.
693 */
694 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
695 error = -posix_acl_chmod(inode, inode->i_mode);
696 if (error)
697 return XFS_ERROR(error);
698 }
699
700 return 0;
701
702 out_trans_cancel:
703 xfs_trans_cancel(tp, 0);
704 xfs_iunlock(ip, XFS_ILOCK_EXCL);
705 out_dqrele:
706 xfs_qm_dqrele(udqp);
707 xfs_qm_dqrele(gdqp);
708 return error;
709 }
710
711 /*
712 * Truncate file. Must have write permission and not be a directory.
713 */
714 int
715 xfs_setattr_size(
716 struct xfs_inode *ip,
717 struct iattr *iattr)
718 {
719 struct xfs_mount *mp = ip->i_mount;
720 struct inode *inode = VFS_I(ip);
721 xfs_off_t oldsize, newsize;
722 struct xfs_trans *tp;
723 int error;
724 uint lock_flags = 0;
725 uint commit_flags = 0;
726
727 trace_xfs_setattr(ip);
728
729 if (mp->m_flags & XFS_MOUNT_RDONLY)
730 return XFS_ERROR(EROFS);
731
732 if (XFS_FORCED_SHUTDOWN(mp))
733 return XFS_ERROR(EIO);
734
735 error = -inode_change_ok(inode, iattr);
736 if (error)
737 return XFS_ERROR(error);
738
739 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
740 ASSERT(S_ISREG(ip->i_d.di_mode));
741 ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
742 ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
743
744 oldsize = inode->i_size;
745 newsize = iattr->ia_size;
746
747 /*
748 * Short circuit the truncate case for zero length files.
749 */
750 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
751 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
752 return 0;
753
754 /*
755 * Use the regular setattr path to update the timestamps.
756 */
757 iattr->ia_valid &= ~ATTR_SIZE;
758 return xfs_setattr_nonsize(ip, iattr, 0);
759 }
760
761 /*
762 * Make sure that the dquots are attached to the inode.
763 */
764 error = xfs_qm_dqattach(ip, 0);
765 if (error)
766 return error;
767
768 /*
769 * Now we can make the changes. Before we join the inode to the
770 * transaction, take care of the part of the truncation that must be
771 * done without the inode lock. This needs to be done before joining
772 * the inode to the transaction, because the inode cannot be unlocked
773 * once it is a part of the transaction.
774 */
775 if (newsize > oldsize) {
776 /*
777 * Do the first part of growing a file: zero any data in the
778 * last block that is beyond the old EOF. We need to do this
779 * before the inode is joined to the transaction to modify
780 * i_size.
781 */
782 error = xfs_zero_eof(ip, newsize, oldsize);
783 if (error)
784 return error;
785 }
786
787 /*
788 * We are going to log the inode size change in this transaction so
789 * any previous writes that are beyond the on disk EOF and the new
790 * EOF that have not been written out need to be written here. If we
791 * do not write the data out, we expose ourselves to the null files
792 * problem.
793 *
794 * Only flush from the on disk size to the smaller of the in memory
795 * file size or the new size as that's the range we really care about
796 * here and prevents waiting for other data not within the range we
797 * care about here.
798 */
799 if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
800 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
801 ip->i_d.di_size, newsize);
802 if (error)
803 return error;
804 }
805
806 /*
807 * Wait for all direct I/O to complete.
808 */
809 inode_dio_wait(inode);
810
811 error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
812 if (error)
813 return error;
814
815 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
816 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
817 if (error)
818 goto out_trans_cancel;
819
820 truncate_setsize(inode, newsize);
821
822 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
823 lock_flags |= XFS_ILOCK_EXCL;
824
825 xfs_ilock(ip, XFS_ILOCK_EXCL);
826
827 xfs_trans_ijoin(tp, ip, 0);
828
829 /*
830 * Only change the c/mtime if we are changing the size or we are
831 * explicitly asked to change it. This handles the semantic difference
832 * between truncate() and ftruncate() as implemented in the VFS.
833 *
834 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
835 * special case where we need to update the times despite not having
836 * these flags set. For all other operations the VFS set these flags
837 * explicitly if it wants a timestamp update.
838 */
839 if (newsize != oldsize &&
840 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
841 iattr->ia_ctime = iattr->ia_mtime =
842 current_fs_time(inode->i_sb);
843 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
844 }
845
846 /*
847 * The first thing we do is set the size to new_size permanently on
848 * disk. This way we don't have to worry about anyone ever being able
849 * to look at the data being freed even in the face of a crash.
850 * What we're getting around here is the case where we free a block, it
851 * is allocated to another file, it is written to, and then we crash.
852 * If the new data gets written to the file but the log buffers
853 * containing the free and reallocation don't, then we'd end up with
854 * garbage in the blocks being freed. As long as we make the new size
855 * permanent before actually freeing any blocks it doesn't matter if
856 * they get written to.
857 */
858 ip->i_d.di_size = newsize;
859 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
860
861 if (newsize <= oldsize) {
862 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
863 if (error)
864 goto out_trans_abort;
865
866 /*
867 * Truncated "down", so we're removing references to old data
868 * here - if we delay flushing for a long time, we expose
869 * ourselves unduly to the notorious NULL files problem. So,
870 * we mark this inode and flush it when the file is closed,
871 * and do not wait the usual (long) time for writeout.
872 */
873 xfs_iflags_set(ip, XFS_ITRUNCATED);
874
875 /* A truncate down always removes post-EOF blocks. */
876 xfs_inode_clear_eofblocks_tag(ip);
877 }
878
879 if (iattr->ia_valid & ATTR_MODE)
880 xfs_setattr_mode(ip, iattr);
881 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
882 xfs_setattr_time(ip, iattr);
883
884 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
885
886 XFS_STATS_INC(xs_ig_attrchg);
887
888 if (mp->m_flags & XFS_MOUNT_WSYNC)
889 xfs_trans_set_sync(tp);
890
891 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
892 out_unlock:
893 if (lock_flags)
894 xfs_iunlock(ip, lock_flags);
895 return error;
896
897 out_trans_abort:
898 commit_flags |= XFS_TRANS_ABORT;
899 out_trans_cancel:
900 xfs_trans_cancel(tp, commit_flags);
901 goto out_unlock;
902 }
903
904 STATIC int
905 xfs_vn_setattr(
906 struct dentry *dentry,
907 struct iattr *iattr)
908 {
909 struct xfs_inode *ip = XFS_I(dentry->d_inode);
910 int error;
911
912 if (iattr->ia_valid & ATTR_SIZE) {
913 xfs_ilock(ip, XFS_IOLOCK_EXCL);
914 error = xfs_setattr_size(ip, iattr);
915 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
916 } else {
917 error = xfs_setattr_nonsize(ip, iattr, 0);
918 }
919
920 return -error;
921 }
922
923 STATIC int
924 xfs_vn_update_time(
925 struct inode *inode,
926 struct timespec *now,
927 int flags)
928 {
929 struct xfs_inode *ip = XFS_I(inode);
930 struct xfs_mount *mp = ip->i_mount;
931 struct xfs_trans *tp;
932 int error;
933
934 trace_xfs_update_time(ip);
935
936 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
937 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
938 if (error) {
939 xfs_trans_cancel(tp, 0);
940 return -error;
941 }
942
943 xfs_ilock(ip, XFS_ILOCK_EXCL);
944 if (flags & S_CTIME) {
945 inode->i_ctime = *now;
946 ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
947 ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
948 }
949 if (flags & S_MTIME) {
950 inode->i_mtime = *now;
951 ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
952 ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
953 }
954 if (flags & S_ATIME) {
955 inode->i_atime = *now;
956 ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
957 ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
958 }
959 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
960 xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
961 return -xfs_trans_commit(tp, 0);
962 }
963
964 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
965
966 /*
967 * Call fiemap helper to fill in user data.
968 * Returns positive errors to xfs_getbmap.
969 */
970 STATIC int
971 xfs_fiemap_format(
972 void **arg,
973 struct getbmapx *bmv,
974 int *full)
975 {
976 int error;
977 struct fiemap_extent_info *fieinfo = *arg;
978 u32 fiemap_flags = 0;
979 u64 logical, physical, length;
980
981 /* Do nothing for a hole */
982 if (bmv->bmv_block == -1LL)
983 return 0;
984
985 logical = BBTOB(bmv->bmv_offset);
986 physical = BBTOB(bmv->bmv_block);
987 length = BBTOB(bmv->bmv_length);
988
989 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
990 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
991 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
992 fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
993 FIEMAP_EXTENT_UNKNOWN);
994 physical = 0; /* no block yet */
995 }
996 if (bmv->bmv_oflags & BMV_OF_LAST)
997 fiemap_flags |= FIEMAP_EXTENT_LAST;
998
999 error = fiemap_fill_next_extent(fieinfo, logical, physical,
1000 length, fiemap_flags);
1001 if (error > 0) {
1002 error = 0;
1003 *full = 1; /* user array now full */
1004 }
1005
1006 return -error;
1007 }
1008
1009 STATIC int
1010 xfs_vn_fiemap(
1011 struct inode *inode,
1012 struct fiemap_extent_info *fieinfo,
1013 u64 start,
1014 u64 length)
1015 {
1016 xfs_inode_t *ip = XFS_I(inode);
1017 struct getbmapx bm;
1018 int error;
1019
1020 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
1021 if (error)
1022 return error;
1023
1024 /* Set up bmap header for xfs internal routine */
1025 bm.bmv_offset = BTOBB(start);
1026 /* Special case for whole file */
1027 if (length == FIEMAP_MAX_OFFSET)
1028 bm.bmv_length = -1LL;
1029 else
1030 bm.bmv_length = BTOBB(length);
1031
1032 /* We add one because in getbmap world count includes the header */
1033 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
1034 fieinfo->fi_extents_max + 1;
1035 bm.bmv_count = min_t(__s32, bm.bmv_count,
1036 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
1037 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
1038 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
1039 bm.bmv_iflags |= BMV_IF_ATTRFORK;
1040 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
1041 bm.bmv_iflags |= BMV_IF_DELALLOC;
1042
1043 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
1044 if (error)
1045 return -error;
1046
1047 return 0;
1048 }
1049
1050 STATIC int
1051 xfs_vn_tmpfile(
1052 struct inode *dir,
1053 struct dentry *dentry,
1054 umode_t mode)
1055 {
1056 int error;
1057
1058 error = xfs_create_tmpfile(XFS_I(dir), dentry, mode);
1059
1060 return -error;
1061 }
1062
1063 static const struct inode_operations xfs_inode_operations = {
1064 .get_acl = xfs_get_acl,
1065 .set_acl = xfs_set_acl,
1066 .getattr = xfs_vn_getattr,
1067 .setattr = xfs_vn_setattr,
1068 .setxattr = generic_setxattr,
1069 .getxattr = generic_getxattr,
1070 .removexattr = generic_removexattr,
1071 .listxattr = xfs_vn_listxattr,
1072 .fiemap = xfs_vn_fiemap,
1073 .update_time = xfs_vn_update_time,
1074 };
1075
1076 static const struct inode_operations xfs_dir_inode_operations = {
1077 .create = xfs_vn_create,
1078 .lookup = xfs_vn_lookup,
1079 .link = xfs_vn_link,
1080 .unlink = xfs_vn_unlink,
1081 .symlink = xfs_vn_symlink,
1082 .mkdir = xfs_vn_mkdir,
1083 /*
1084 * Yes, XFS uses the same method for rmdir and unlink.
1085 *
1086 * There are some subtile differences deeper in the code,
1087 * but we use S_ISDIR to check for those.
1088 */
1089 .rmdir = xfs_vn_unlink,
1090 .mknod = xfs_vn_mknod,
1091 .rename = xfs_vn_rename,
1092 .get_acl = xfs_get_acl,
1093 .set_acl = xfs_set_acl,
1094 .getattr = xfs_vn_getattr,
1095 .setattr = xfs_vn_setattr,
1096 .setxattr = generic_setxattr,
1097 .getxattr = generic_getxattr,
1098 .removexattr = generic_removexattr,
1099 .listxattr = xfs_vn_listxattr,
1100 .update_time = xfs_vn_update_time,
1101 .tmpfile = xfs_vn_tmpfile,
1102 };
1103
1104 static const struct inode_operations xfs_dir_ci_inode_operations = {
1105 .create = xfs_vn_create,
1106 .lookup = xfs_vn_ci_lookup,
1107 .link = xfs_vn_link,
1108 .unlink = xfs_vn_unlink,
1109 .symlink = xfs_vn_symlink,
1110 .mkdir = xfs_vn_mkdir,
1111 /*
1112 * Yes, XFS uses the same method for rmdir and unlink.
1113 *
1114 * There are some subtile differences deeper in the code,
1115 * but we use S_ISDIR to check for those.
1116 */
1117 .rmdir = xfs_vn_unlink,
1118 .mknod = xfs_vn_mknod,
1119 .rename = xfs_vn_rename,
1120 .get_acl = xfs_get_acl,
1121 .set_acl = xfs_set_acl,
1122 .getattr = xfs_vn_getattr,
1123 .setattr = xfs_vn_setattr,
1124 .setxattr = generic_setxattr,
1125 .getxattr = generic_getxattr,
1126 .removexattr = generic_removexattr,
1127 .listxattr = xfs_vn_listxattr,
1128 .update_time = xfs_vn_update_time,
1129 .tmpfile = xfs_vn_tmpfile,
1130 };
1131
1132 static const struct inode_operations xfs_symlink_inode_operations = {
1133 .readlink = generic_readlink,
1134 .follow_link = xfs_vn_follow_link,
1135 .put_link = kfree_put_link,
1136 .getattr = xfs_vn_getattr,
1137 .setattr = xfs_vn_setattr,
1138 .setxattr = generic_setxattr,
1139 .getxattr = generic_getxattr,
1140 .removexattr = generic_removexattr,
1141 .listxattr = xfs_vn_listxattr,
1142 .update_time = xfs_vn_update_time,
1143 };
1144
1145 STATIC void
1146 xfs_diflags_to_iflags(
1147 struct inode *inode,
1148 struct xfs_inode *ip)
1149 {
1150 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
1151 inode->i_flags |= S_IMMUTABLE;
1152 else
1153 inode->i_flags &= ~S_IMMUTABLE;
1154 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
1155 inode->i_flags |= S_APPEND;
1156 else
1157 inode->i_flags &= ~S_APPEND;
1158 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
1159 inode->i_flags |= S_SYNC;
1160 else
1161 inode->i_flags &= ~S_SYNC;
1162 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
1163 inode->i_flags |= S_NOATIME;
1164 else
1165 inode->i_flags &= ~S_NOATIME;
1166 }
1167
1168 /*
1169 * Initialize the Linux inode, set up the operation vectors and
1170 * unlock the inode.
1171 *
1172 * When reading existing inodes from disk this is called directly
1173 * from xfs_iget, when creating a new inode it is called from
1174 * xfs_ialloc after setting up the inode.
1175 *
1176 * We are always called with an uninitialised linux inode here.
1177 * We need to initialise the necessary fields and take a reference
1178 * on it.
1179 */
1180 void
1181 xfs_setup_inode(
1182 struct xfs_inode *ip)
1183 {
1184 struct inode *inode = &ip->i_vnode;
1185 gfp_t gfp_mask;
1186
1187 inode->i_ino = ip->i_ino;
1188 inode->i_state = I_NEW;
1189
1190 inode_sb_list_add(inode);
1191 /* make the inode look hashed for the writeback code */
1192 hlist_add_fake(&inode->i_hash);
1193
1194 inode->i_mode = ip->i_d.di_mode;
1195 set_nlink(inode, ip->i_d.di_nlink);
1196 inode->i_uid = xfs_uid_to_kuid(ip->i_d.di_uid);
1197 inode->i_gid = xfs_gid_to_kgid(ip->i_d.di_gid);
1198
1199 switch (inode->i_mode & S_IFMT) {
1200 case S_IFBLK:
1201 case S_IFCHR:
1202 inode->i_rdev =
1203 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
1204 sysv_minor(ip->i_df.if_u2.if_rdev));
1205 break;
1206 default:
1207 inode->i_rdev = 0;
1208 break;
1209 }
1210
1211 inode->i_generation = ip->i_d.di_gen;
1212 i_size_write(inode, ip->i_d.di_size);
1213 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
1214 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
1215 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
1216 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
1217 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
1218 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
1219 xfs_diflags_to_iflags(inode, ip);
1220
1221 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
1222 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
1223 switch (inode->i_mode & S_IFMT) {
1224 case S_IFREG:
1225 inode->i_op = &xfs_inode_operations;
1226 inode->i_fop = &xfs_file_operations;
1227 inode->i_mapping->a_ops = &xfs_address_space_operations;
1228 break;
1229 case S_IFDIR:
1230 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
1231 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1232 inode->i_op = &xfs_dir_ci_inode_operations;
1233 else
1234 inode->i_op = &xfs_dir_inode_operations;
1235 inode->i_fop = &xfs_dir_file_operations;
1236 ip->d_ops = ip->i_mount->m_dir_inode_ops;
1237 break;
1238 case S_IFLNK:
1239 inode->i_op = &xfs_symlink_inode_operations;
1240 if (!(ip->i_df.if_flags & XFS_IFINLINE))
1241 inode->i_mapping->a_ops = &xfs_address_space_operations;
1242 break;
1243 default:
1244 inode->i_op = &xfs_inode_operations;
1245 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1246 break;
1247 }
1248
1249 /*
1250 * Ensure all page cache allocations are done from GFP_NOFS context to
1251 * prevent direct reclaim recursion back into the filesystem and blowing
1252 * stacks or deadlocking.
1253 */
1254 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1255 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1256
1257 /*
1258 * If there is no attribute fork no ACL can exist on this inode,
1259 * and it can't have any file capabilities attached to it either.
1260 */
1261 if (!XFS_IFORK_Q(ip)) {
1262 inode_has_no_xattr(inode);
1263 cache_no_acl(inode);
1264 }
1265
1266 xfs_iflags_clear(ip, XFS_INEW);
1267 barrier();
1268
1269 unlock_new_inode(inode);
1270 }