Merge branch 'akpm' (incoming from Andrew)
[deliverable/linux.git] / fs / xfs / xfs_iops.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4
LT
18#include "xfs.h"
19#include "xfs_fs.h"
70a9883c 20#include "xfs_shared.h"
239880ef
DC
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
1da177e4
LT
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4 26#include "xfs_mount.h"
57062787 27#include "xfs_da_format.h"
1da177e4
LT
28#include "xfs_inode.h"
29#include "xfs_bmap.h"
68988114 30#include "xfs_bmap_util.h"
239880ef 31#include "xfs_acl.h"
239880ef 32#include "xfs_quota.h"
1da177e4 33#include "xfs_error.h"
1da177e4 34#include "xfs_attr.h"
239880ef 35#include "xfs_trans.h"
0b1b213f 36#include "xfs_trace.h"
27b52867 37#include "xfs_icache.h"
c24b5dfa 38#include "xfs_symlink.h"
0cb97766 39#include "xfs_da_btree.h"
0cb97766 40#include "xfs_dir2_priv.h"
a4fbe6ab 41#include "xfs_dinode.h"
99b6436b 42#include "xfs_trans_space.h"
1da177e4 43
16f7e0fe 44#include <linux/capability.h>
1da177e4
LT
45#include <linux/xattr.h>
46#include <linux/namei.h>
ef14f0c1 47#include <linux/posix_acl.h>
446ada4a 48#include <linux/security.h>
f35642e2 49#include <linux/fiemap.h>
5a0e3ad6 50#include <linux/slab.h>
1da177e4 51
93a8614e
DC
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 */
61static struct lock_class_key xfs_nondir_ilock_class;
62static struct lock_class_key xfs_dir_ilock_class;
63
8d2a5e6e
DC
64static int
65xfs_initxattrs(
66 struct inode *inode,
67 const struct xattr *xattr_array,
68 void *fs_info)
9d8f13ba 69{
8d2a5e6e
DC
70 const struct xattr *xattr;
71 struct xfs_inode *ip = XFS_I(inode);
72 int error = 0;
9d8f13ba
MZ
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
446ada4a
NS
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 */
9d8f13ba 89
446ada4a 90STATIC int
416c6d5b 91xfs_init_security(
af048193 92 struct inode *inode,
2a7dba39
EP
93 struct inode *dir,
94 const struct qstr *qstr)
446ada4a 95{
9d8f13ba
MZ
96 return security_inode_init_security(inode, dir, qstr,
97 &xfs_initxattrs, NULL);
446ada4a
NS
98}
99
556b8b16
BN
100static void
101xfs_dentry_to_name(
102 struct xfs_name *namep,
0cb97766
DC
103 struct dentry *dentry,
104 int mode)
556b8b16
BN
105{
106 namep->name = dentry->d_name.name;
107 namep->len = dentry->d_name.len;
0cb97766 108 namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
556b8b16
BN
109}
110
7989cb8e 111STATIC void
416c6d5b 112xfs_cleanup_inode(
739bfb2a 113 struct inode *dir,
af048193 114 struct inode *inode,
8f112e3b 115 struct dentry *dentry)
3a69c7dc 116{
556b8b16 117 struct xfs_name teardown;
3a69c7dc
YL
118
119 /* Oh, the horror.
220b5284 120 * If we can't add the ACL or we fail in
416c6d5b 121 * xfs_init_security we must back out.
3a69c7dc
YL
122 * ENOSPC can hit here, among other things.
123 */
0cb97766 124 xfs_dentry_to_name(&teardown, dentry, 0);
3a69c7dc 125
8f112e3b 126 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
af048193 127 iput(inode);
3a69c7dc
YL
128}
129
1da177e4 130STATIC int
416c6d5b 131xfs_vn_mknod(
1da177e4
LT
132 struct inode *dir,
133 struct dentry *dentry,
1a67aafb 134 umode_t mode,
1da177e4
LT
135 dev_t rdev)
136{
db0bb7ba 137 struct inode *inode;
979ebab1 138 struct xfs_inode *ip = NULL;
2401dc29 139 struct posix_acl *default_acl, *acl;
556b8b16 140 struct xfs_name name;
1da177e4
LT
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 */
517b5e8c
CH
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 }
1da177e4 154
2401dc29
CH
155 error = posix_acl_create(dir, &mode, &default_acl, &acl);
156 if (error)
157 return error;
1da177e4 158
0cb97766 159 xfs_dentry_to_name(&name, dentry, mode);
6c77b0ea 160 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
db0bb7ba
CH
161 if (unlikely(error))
162 goto out_free_acl;
446ada4a 163
01651646 164 inode = VFS_I(ip);
979ebab1 165
2a7dba39 166 error = xfs_init_security(inode, dir, &dentry->d_name);
db0bb7ba
CH
167 if (unlikely(error))
168 goto out_cleanup_inode;
169
2401dc29 170#ifdef CONFIG_XFS_POSIX_ACL
db0bb7ba 171 if (default_acl) {
2401dc29
CH
172 error = xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
173 if (error)
db0bb7ba 174 goto out_cleanup_inode;
1da177e4 175 }
2401dc29
CH
176 if (acl) {
177 error = xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
178 if (error)
179 goto out_cleanup_inode;
180 }
181#endif
1da177e4 182
db0bb7ba 183 d_instantiate(dentry, inode);
2401dc29
CH
184 out_free_acl:
185 if (default_acl)
186 posix_acl_release(default_acl);
187 if (acl)
188 posix_acl_release(acl);
db0bb7ba
CH
189 return -error;
190
191 out_cleanup_inode:
8f112e3b 192 xfs_cleanup_inode(dir, inode, dentry);
2401dc29 193 goto out_free_acl;
1da177e4
LT
194}
195
196STATIC int
416c6d5b 197xfs_vn_create(
1da177e4
LT
198 struct inode *dir,
199 struct dentry *dentry,
4acdaf27 200 umode_t mode,
ebfc3b49 201 bool flags)
1da177e4 202{
416c6d5b 203 return xfs_vn_mknod(dir, dentry, mode, 0);
1da177e4
LT
204}
205
206STATIC int
416c6d5b 207xfs_vn_mkdir(
1da177e4
LT
208 struct inode *dir,
209 struct dentry *dentry,
18bb1db3 210 umode_t mode)
1da177e4 211{
416c6d5b 212 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
1da177e4
LT
213}
214
215STATIC struct dentry *
416c6d5b 216xfs_vn_lookup(
1da177e4
LT
217 struct inode *dir,
218 struct dentry *dentry,
00cd8dd3 219 unsigned int flags)
1da177e4 220{
ef1f5e7a 221 struct xfs_inode *cip;
556b8b16 222 struct xfs_name name;
1da177e4
LT
223 int error;
224
225 if (dentry->d_name.len >= MAXNAMELEN)
226 return ERR_PTR(-ENAMETOOLONG);
227
0cb97766 228 xfs_dentry_to_name(&name, dentry, 0);
384f3ced 229 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
67fcaa73 230 if (unlikely(error)) {
1da177e4
LT
231 if (unlikely(error != ENOENT))
232 return ERR_PTR(-error);
233 d_add(dentry, NULL);
234 return NULL;
235 }
236
01651646 237 return d_splice_alias(VFS_I(cip), dentry);
1da177e4
LT
238}
239
384f3ced
BN
240STATIC struct dentry *
241xfs_vn_ci_lookup(
242 struct inode *dir,
243 struct dentry *dentry,
00cd8dd3 244 unsigned int flags)
384f3ced
BN
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
0cb97766 255 xfs_dentry_to_name(&xname, dentry, 0);
384f3ced
BN
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);
866d5dc9
BN
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 */
384f3ced
BN
265 return NULL;
266 }
267
268 /* if exact match, just splice and exit */
269 if (!ci_name.name)
01651646 270 return d_splice_alias(VFS_I(ip), dentry);
384f3ced
BN
271
272 /* else case-insensitive match... */
273 dname.name = ci_name.name;
274 dname.len = ci_name.len;
e45b590b 275 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
384f3ced
BN
276 kmem_free(ci_name.name);
277 return dentry;
278}
279
1da177e4 280STATIC int
416c6d5b 281xfs_vn_link(
1da177e4
LT
282 struct dentry *old_dentry,
283 struct inode *dir,
284 struct dentry *dentry)
285{
d9424b3c 286 struct inode *inode = old_dentry->d_inode;
556b8b16 287 struct xfs_name name;
1da177e4
LT
288 int error;
289
0cb97766 290 xfs_dentry_to_name(&name, dentry, inode->i_mode);
1da177e4 291
556b8b16 292 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
d9424b3c 293 if (unlikely(error))
a3da7896 294 return -error;
a3da7896 295
7de9c6ee 296 ihold(inode);
a3da7896
CH
297 d_instantiate(dentry, inode);
298 return 0;
1da177e4
LT
299}
300
301STATIC int
416c6d5b 302xfs_vn_unlink(
1da177e4
LT
303 struct inode *dir,
304 struct dentry *dentry)
305{
556b8b16 306 struct xfs_name name;
1da177e4
LT
307 int error;
308
0cb97766 309 xfs_dentry_to_name(&name, dentry, 0);
1da177e4 310
e5700704
CH
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;
1da177e4
LT
323}
324
325STATIC int
416c6d5b 326xfs_vn_symlink(
1da177e4
LT
327 struct inode *dir,
328 struct dentry *dentry,
329 const char *symname)
330{
3937be5b
CH
331 struct inode *inode;
332 struct xfs_inode *cip = NULL;
556b8b16 333 struct xfs_name name;
1da177e4 334 int error;
576b1d67 335 umode_t mode;
1da177e4 336
3e5daf05 337 mode = S_IFLNK |
ce3b0f8d 338 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
0cb97766 339 xfs_dentry_to_name(&name, dentry, mode);
1da177e4 340
6c77b0ea 341 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
3937be5b
CH
342 if (unlikely(error))
343 goto out;
344
01651646 345 inode = VFS_I(cip);
3937be5b 346
2a7dba39 347 error = xfs_init_security(inode, dir, &dentry->d_name);
3937be5b
CH
348 if (unlikely(error))
349 goto out_cleanup_inode;
350
351 d_instantiate(dentry, inode);
3937be5b
CH
352 return 0;
353
354 out_cleanup_inode:
8f112e3b 355 xfs_cleanup_inode(dir, inode, dentry);
3937be5b 356 out:
1da177e4
LT
357 return -error;
358}
359
1da177e4 360STATIC int
416c6d5b 361xfs_vn_rename(
1da177e4
LT
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;
556b8b16
BN
368 struct xfs_name oname;
369 struct xfs_name nname;
1da177e4 370
0cb97766
DC
371 xfs_dentry_to_name(&oname, odentry, 0);
372 xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
556b8b16 373
e5700704 374 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
cfa853e4 375 XFS_I(ndir), &nname, new_inode ?
0cb97766 376 XFS_I(new_inode) : NULL);
1da177e4
LT
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 */
008b150a 384STATIC void *
416c6d5b 385xfs_vn_follow_link(
1da177e4
LT
386 struct dentry *dentry,
387 struct nameidata *nd)
388{
1da177e4 389 char *link;
804c83c3 390 int error = -ENOMEM;
1da177e4 391
f52720ca 392 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
804c83c3
CH
393 if (!link)
394 goto out_err;
1da177e4 395
739bfb2a 396 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
804c83c3
CH
397 if (unlikely(error))
398 goto out_kfree;
1da177e4
LT
399
400 nd_set_link(nd, link);
008b150a 401 return NULL;
804c83c3
CH
402
403 out_kfree:
404 kfree(link);
405 out_err:
406 nd_set_link(nd, ERR_PTR(error));
407 return NULL;
1da177e4
LT
408}
409
1da177e4 410STATIC int
416c6d5b 411xfs_vn_getattr(
c43f4087
CH
412 struct vfsmount *mnt,
413 struct dentry *dentry,
414 struct kstat *stat)
1da177e4 415{
c43f4087
CH
416 struct inode *inode = dentry->d_inode;
417 struct xfs_inode *ip = XFS_I(inode);
418 struct xfs_mount *mp = ip->i_mount;
419
cca28fb8 420 trace_xfs_getattr(ip);
c43f4087
CH
421
422 if (XFS_FORCED_SHUTDOWN(mp))
ed32201e 423 return -XFS_ERROR(EIO);
c43f4087
CH
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;
7aab1b28
DE
429 stat->uid = inode->i_uid;
430 stat->gid = inode->i_gid;
c43f4087 431 stat->ino = ip->i_ino;
c43f4087 432 stat->atime = inode->i_atime;
f9581b14
CH
433 stat->mtime = inode->i_mtime;
434 stat->ctime = inode->i_ctime;
c43f4087
CH
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:
71ddabb9 447 if (XFS_IS_REALTIME_INODE(ip)) {
c43f4087
CH
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;
69e23b9a 459 }
c43f4087
CH
460
461 return 0;
1da177e4
LT
462}
463
56c19e89
DC
464static void
465xfs_setattr_mode(
56c19e89
DC
466 struct xfs_inode *ip,
467 struct iattr *iattr)
468{
0c3d88df
CH
469 struct inode *inode = VFS_I(ip);
470 umode_t mode = iattr->ia_mode;
56c19e89 471
56c19e89
DC
472 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
473
56c19e89
DC
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
c91c46c1
CH
481static void
482xfs_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
c4ed4243
CH
507int
508xfs_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;
7aab1b28
DE
518 kuid_t uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
519 kgid_t gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
c4ed4243
CH
520 struct xfs_dquot *udqp = NULL, *gdqp = NULL;
521 struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
522
523 trace_xfs_setattr(ip);
524
42c49d7f
CM
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);
c4ed4243 529
42c49d7f
CM
530 if (XFS_FORCED_SHUTDOWN(mp))
531 return XFS_ERROR(EIO);
c4ed4243 532
42c49d7f
CM
533 error = -inode_change_ok(inode, iattr);
534 if (error)
535 return XFS_ERROR(error);
536 }
c4ed4243
CH
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 {
7aab1b28 555 uid = inode->i_uid;
c4ed4243
CH
556 }
557 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
558 gid = iattr->ia_gid;
559 qflags |= XFS_QMOPT_GQUOTA;
560 } else {
7aab1b28 561 gid = inode->i_gid;
c4ed4243
CH
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);
7aab1b28
DE
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);
c4ed4243
CH
575 if (error)
576 return error;
577 }
578
579 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
3d3c8b52 580 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
c4ed4243
CH
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 */
7aab1b28
DE
596 iuid = inode->i_uid;
597 igid = inode->i_gid;
c4ed4243
CH
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) &&
7aab1b28
DE
606 ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
607 (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
c4ed4243
CH
608 ASSERT(tp);
609 error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
92f8ff73 610 NULL, capable(CAP_FOWNER) ?
c4ed4243
CH
611 XFS_QMOPT_FORCE_RES : 0);
612 if (error) /* out of quota */
613 goto out_trans_cancel;
614 }
615 }
616
ddc3415a 617 xfs_trans_ijoin(tp, ip, 0);
c4ed4243
CH
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 */
7aab1b28 637 if (!uid_eq(iuid, uid)) {
c4ed4243
CH
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 }
7aab1b28 644 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
c4ed4243
CH
645 inode->i_uid = uid;
646 }
7aab1b28 647 if (!gid_eq(igid, gid)) {
c4ed4243 648 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
5a01dd54
JL
649 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
650 !XFS_IS_PQUOTA_ON(mp));
c4ed4243
CH
651 ASSERT(mask & ATTR_GID);
652 ASSERT(gdqp);
653 olddquot2 = xfs_qm_vop_chown(tp, ip,
654 &ip->i_gdquot, gdqp);
655 }
7aab1b28 656 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
c4ed4243
CH
657 inode->i_gid = gid;
658 }
659 }
660
56c19e89 661 if (mask & ATTR_MODE)
0c3d88df 662 xfs_setattr_mode(ip, iattr);
c91c46c1
CH
663 if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
664 xfs_setattr_time(ip, iattr);
c4ed4243
CH
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)) {
2401dc29 695 error = -posix_acl_chmod(inode, inode->i_mode);
c4ed4243
CH
696 if (error)
697 return XFS_ERROR(error);
698 }
699
700 return 0;
701
702out_trans_cancel:
703 xfs_trans_cancel(tp, 0);
704 xfs_iunlock(ip, XFS_ILOCK_EXCL);
705out_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 */
714int
715xfs_setattr_size(
716 struct xfs_inode *ip,
76ca4c23 717 struct iattr *iattr)
c4ed4243
CH
718{
719 struct xfs_mount *mp = ip->i_mount;
720 struct inode *inode = VFS_I(ip);
673e8e59 721 xfs_off_t oldsize, newsize;
c4ed4243
CH
722 struct xfs_trans *tp;
723 int error;
f38996f5 724 uint lock_flags = 0;
c4ed4243
CH
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
76ca4c23 739 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
c4ed4243 740 ASSERT(S_ISREG(ip->i_d.di_mode));
fe60a8a0
CH
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);
c4ed4243 743
ce7ae151 744 oldsize = inode->i_size;
673e8e59
CH
745 newsize = iattr->ia_size;
746
c4ed4243
CH
747 /*
748 * Short circuit the truncate case for zero length files.
749 */
673e8e59 750 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
fe60a8a0 751 if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
76ca4c23 752 return 0;
681b1200
CH
753
754 /*
755 * Use the regular setattr path to update the timestamps.
756 */
681b1200
CH
757 iattr->ia_valid &= ~ATTR_SIZE;
758 return xfs_setattr_nonsize(ip, iattr, 0);
c4ed4243
CH
759 }
760
761 /*
762 * Make sure that the dquots are attached to the inode.
763 */
f38996f5 764 error = xfs_qm_dqattach(ip, 0);
c4ed4243 765 if (error)
76ca4c23 766 return error;
c4ed4243
CH
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 */
673e8e59 775 if (newsize > oldsize) {
c4ed4243
CH
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 */
673e8e59 782 error = xfs_zero_eof(ip, newsize, oldsize);
c4ed4243 783 if (error)
76ca4c23 784 return error;
c4ed4243 785 }
c4ed4243
CH
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 */
673e8e59 799 if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
4bc1ea6b
DC
800 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
801 ip->i_d.di_size, newsize);
c4ed4243 802 if (error)
76ca4c23 803 return error;
c4ed4243
CH
804 }
805
806 /*
4a06fd26 807 * Wait for all direct I/O to complete.
c4ed4243 808 */
4a06fd26 809 inode_dio_wait(inode);
c4ed4243 810
673e8e59 811 error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
c4ed4243 812 if (error)
76ca4c23 813 return error;
c4ed4243
CH
814
815 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
3d3c8b52 816 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
c4ed4243
CH
817 if (error)
818 goto out_trans_cancel;
819
673e8e59 820 truncate_setsize(inode, newsize);
c4ed4243
CH
821
822 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
823 lock_flags |= XFS_ILOCK_EXCL;
824
825 xfs_ilock(ip, XFS_ILOCK_EXCL);
826
ddc3415a 827 xfs_trans_ijoin(tp, ip, 0);
c4ed4243
CH
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 */
fe60a8a0
CH
839 if (newsize != oldsize &&
840 !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
c4ed4243
CH
841 iattr->ia_ctime = iattr->ia_mtime =
842 current_fs_time(inode->i_sb);
fe60a8a0 843 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
c4ed4243
CH
844 }
845
673e8e59
CH
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;
673e8e59
CH
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);
c4ed4243
CH
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);
27b52867
BF
874
875 /* A truncate down always removes post-EOF blocks. */
876 xfs_inode_clear_eofblocks_tag(ip);
c4ed4243
CH
877 }
878
fe60a8a0 879 if (iattr->ia_valid & ATTR_MODE)
0c3d88df 880 xfs_setattr_mode(ip, iattr);
fe60a8a0 881 if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
c91c46c1 882 xfs_setattr_time(ip, iattr);
c4ed4243
CH
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);
892out_unlock:
893 if (lock_flags)
894 xfs_iunlock(ip, lock_flags);
895 return error;
896
897out_trans_abort:
898 commit_flags |= XFS_TRANS_ABORT;
899out_trans_cancel:
900 xfs_trans_cancel(tp, commit_flags);
901 goto out_unlock;
902}
903
1da177e4 904STATIC int
416c6d5b 905xfs_vn_setattr(
76ca4c23
CH
906 struct dentry *dentry,
907 struct iattr *iattr)
1da177e4 908{
76ca4c23
CH
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;
1da177e4
LT
921}
922
69ff2826
CH
923STATIC int
924xfs_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);
3d3c8b52 937 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
69ff2826
CH
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
f35642e2
ES
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 */
970STATIC int
971xfs_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) {
635c4d0b
JL
992 fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
993 FIEMAP_EXTENT_UNKNOWN);
f35642e2
ES
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
1009STATIC int
1010xfs_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
97db39a1 1032 /* We add one because in getbmap world count includes the header */
2d1ff3c7
TM
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)));
9af25465 1037 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
f35642e2
ES
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
99b6436b
ZYW
1050STATIC int
1051xfs_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
41be8bed 1063static const struct inode_operations xfs_inode_operations = {
4e34e719 1064 .get_acl = xfs_get_acl,
2401dc29 1065 .set_acl = xfs_set_acl,
416c6d5b
NS
1066 .getattr = xfs_vn_getattr,
1067 .setattr = xfs_vn_setattr,
0ec58516
LM
1068 .setxattr = generic_setxattr,
1069 .getxattr = generic_getxattr,
1070 .removexattr = generic_removexattr,
416c6d5b 1071 .listxattr = xfs_vn_listxattr,
f35642e2 1072 .fiemap = xfs_vn_fiemap,
69ff2826 1073 .update_time = xfs_vn_update_time,
1da177e4
LT
1074};
1075
41be8bed 1076static const struct inode_operations xfs_dir_inode_operations = {
416c6d5b
NS
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,
8f112e3b
CH
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,
416c6d5b
NS
1090 .mknod = xfs_vn_mknod,
1091 .rename = xfs_vn_rename,
4e34e719 1092 .get_acl = xfs_get_acl,
2401dc29 1093 .set_acl = xfs_set_acl,
416c6d5b
NS
1094 .getattr = xfs_vn_getattr,
1095 .setattr = xfs_vn_setattr,
0ec58516
LM
1096 .setxattr = generic_setxattr,
1097 .getxattr = generic_getxattr,
1098 .removexattr = generic_removexattr,
416c6d5b 1099 .listxattr = xfs_vn_listxattr,
69ff2826 1100 .update_time = xfs_vn_update_time,
99b6436b 1101 .tmpfile = xfs_vn_tmpfile,
1da177e4
LT
1102};
1103
41be8bed 1104static const struct inode_operations xfs_dir_ci_inode_operations = {
384f3ced
BN
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,
8f112e3b
CH
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,
384f3ced
BN
1118 .mknod = xfs_vn_mknod,
1119 .rename = xfs_vn_rename,
4e34e719 1120 .get_acl = xfs_get_acl,
2401dc29 1121 .set_acl = xfs_set_acl,
384f3ced
BN
1122 .getattr = xfs_vn_getattr,
1123 .setattr = xfs_vn_setattr,
0ec58516
LM
1124 .setxattr = generic_setxattr,
1125 .getxattr = generic_getxattr,
1126 .removexattr = generic_removexattr,
384f3ced 1127 .listxattr = xfs_vn_listxattr,
69ff2826 1128 .update_time = xfs_vn_update_time,
99b6436b 1129 .tmpfile = xfs_vn_tmpfile,
384f3ced
BN
1130};
1131
41be8bed 1132static const struct inode_operations xfs_symlink_inode_operations = {
1da177e4 1133 .readlink = generic_readlink,
416c6d5b 1134 .follow_link = xfs_vn_follow_link,
96c8c442 1135 .put_link = kfree_put_link,
416c6d5b
NS
1136 .getattr = xfs_vn_getattr,
1137 .setattr = xfs_vn_setattr,
0ec58516
LM
1138 .setxattr = generic_setxattr,
1139 .getxattr = generic_getxattr,
1140 .removexattr = generic_removexattr,
416c6d5b 1141 .listxattr = xfs_vn_listxattr,
69ff2826 1142 .update_time = xfs_vn_update_time,
1da177e4 1143};
41be8bed
CH
1144
1145STATIC void
1146xfs_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.
bf904248
DC
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.
41be8bed
CH
1179 */
1180void
1181xfs_setup_inode(
1182 struct xfs_inode *ip)
1183{
bf904248 1184 struct inode *inode = &ip->i_vnode;
ad22c7a0 1185 gfp_t gfp_mask;
bf904248
DC
1186
1187 inode->i_ino = ip->i_ino;
eaff8079 1188 inode->i_state = I_NEW;
646ec461
CH
1189
1190 inode_sb_list_add(inode);
c6f6cd06
CH
1191 /* make the inode look hashed for the writeback code */
1192 hlist_add_fake(&inode->i_hash);
41be8bed
CH
1193
1194 inode->i_mode = ip->i_d.di_mode;
bfe86848 1195 set_nlink(inode, ip->i_d.di_nlink);
7aab1b28
DE
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);
41be8bed
CH
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);
41be8bed 1220
4bceb18f 1221 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
93a8614e 1222 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
41be8bed
CH
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:
93a8614e 1230 lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
41be8bed
CH
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;
32c5483a 1236 ip->d_ops = ip->i_mount->m_dir_inode_ops;
41be8bed
CH
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
ad22c7a0
DC
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
510792ee
CH
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);
6311b108 1263 cache_no_acl(inode);
510792ee 1264 }
6311b108 1265
41be8bed
CH
1266 xfs_iflags_clear(ip, XFS_INEW);
1267 barrier();
1268
1269 unlock_new_inode(inode);
1270}
This page took 0.845682 seconds and 5 git commands to generate.