fallocate should be a file operation
[linux-2.6-block.git] / fs / xfs / linux-2.6 / 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"
ef14f0c1 20#include "xfs_acl.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4 27#include "xfs_alloc.h"
1da177e4
LT
28#include "xfs_quota.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
1da177e4
LT
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
1da177e4
LT
34#include "xfs_rtalloc.h"
35#include "xfs_error.h"
36#include "xfs_itable.h"
37#include "xfs_rw.h"
1da177e4
LT
38#include "xfs_attr.h"
39#include "xfs_buf_item.h"
40#include "xfs_utils.h"
739bfb2a 41#include "xfs_vnodeops.h"
0b1b213f 42#include "xfs_trace.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
42fe2b1f 52/*
f9581b14
CH
53 * Bring the timestamps in the XFS inode uptodate.
54 *
55 * Used before writing the inode to disk.
42fe2b1f
CH
56 */
57void
f9581b14 58xfs_synchronize_times(
42fe2b1f
CH
59 xfs_inode_t *ip)
60{
01651646 61 struct inode *inode = VFS_I(ip);
42fe2b1f 62
f9581b14
CH
63 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
64 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
65 ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec;
66 ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec;
67 ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec;
68 ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec;
42fe2b1f
CH
69}
70
5d51eff4 71/*
bf904248 72 * If the linux inode is valid, mark it dirty.
5d51eff4
DC
73 * Used when commiting a dirty inode into a transaction so that
74 * the inode will get written back by the linux code
75 */
76void
77xfs_mark_inode_dirty_sync(
78 xfs_inode_t *ip)
79{
01651646 80 struct inode *inode = VFS_I(ip);
5d51eff4 81
a4ffdde6 82 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
af048193 83 mark_inode_dirty_sync(inode);
5d51eff4
DC
84}
85
66d834ea
CH
86void
87xfs_mark_inode_dirty(
88 xfs_inode_t *ip)
89{
90 struct inode *inode = VFS_I(ip);
91
a4ffdde6 92 if (!(inode->i_state & (I_WILL_FREE|I_FREEING)))
66d834ea
CH
93 mark_inode_dirty(inode);
94}
95
446ada4a
NS
96/*
97 * Hook in SELinux. This is not quite correct yet, what we really need
98 * here (as we do for default ACLs) is a mechanism by which creation of
99 * these attrs can be journalled at inode creation time (along with the
100 * inode, of course, such that log replay can't cause these to be lost).
101 */
102STATIC int
416c6d5b 103xfs_init_security(
af048193 104 struct inode *inode,
446ada4a
NS
105 struct inode *dir)
106{
af048193 107 struct xfs_inode *ip = XFS_I(inode);
446ada4a
NS
108 size_t length;
109 void *value;
a9273ca5 110 unsigned char *name;
446ada4a
NS
111 int error;
112
a9273ca5 113 error = security_inode_init_security(inode, dir, (char **)&name,
af048193 114 &value, &length);
446ada4a
NS
115 if (error) {
116 if (error == -EOPNOTSUPP)
117 return 0;
118 return -error;
119 }
120
af048193 121 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
446ada4a
NS
122
123 kfree(name);
124 kfree(value);
125 return error;
126}
127
556b8b16
BN
128static void
129xfs_dentry_to_name(
130 struct xfs_name *namep,
131 struct dentry *dentry)
132{
133 namep->name = dentry->d_name.name;
134 namep->len = dentry->d_name.len;
135}
136
7989cb8e 137STATIC void
416c6d5b 138xfs_cleanup_inode(
739bfb2a 139 struct inode *dir,
af048193 140 struct inode *inode,
8f112e3b 141 struct dentry *dentry)
3a69c7dc 142{
556b8b16 143 struct xfs_name teardown;
3a69c7dc
YL
144
145 /* Oh, the horror.
220b5284 146 * If we can't add the ACL or we fail in
416c6d5b 147 * xfs_init_security we must back out.
3a69c7dc
YL
148 * ENOSPC can hit here, among other things.
149 */
556b8b16 150 xfs_dentry_to_name(&teardown, dentry);
3a69c7dc 151
8f112e3b 152 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
af048193 153 iput(inode);
3a69c7dc
YL
154}
155
1da177e4 156STATIC int
416c6d5b 157xfs_vn_mknod(
1da177e4
LT
158 struct inode *dir,
159 struct dentry *dentry,
160 int mode,
161 dev_t rdev)
162{
db0bb7ba 163 struct inode *inode;
979ebab1 164 struct xfs_inode *ip = NULL;
ef14f0c1 165 struct posix_acl *default_acl = NULL;
556b8b16 166 struct xfs_name name;
1da177e4
LT
167 int error;
168
169 /*
170 * Irix uses Missed'em'V split, but doesn't want to see
171 * the upper 5 bits of (14bit) major.
172 */
517b5e8c
CH
173 if (S_ISCHR(mode) || S_ISBLK(mode)) {
174 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
175 return -EINVAL;
176 rdev = sysv_encode_dev(rdev);
177 } else {
178 rdev = 0;
179 }
1da177e4 180
ef14f0c1
CH
181 if (IS_POSIXACL(dir)) {
182 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
183 if (IS_ERR(default_acl))
184 return -PTR_ERR(default_acl);
1da177e4 185
e83f1eb6
CH
186 if (!default_acl)
187 mode &= ~current_umask();
ef14f0c1 188 }
1da177e4 189
517b5e8c 190 xfs_dentry_to_name(&name, dentry);
6c77b0ea 191 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
db0bb7ba
CH
192 if (unlikely(error))
193 goto out_free_acl;
446ada4a 194
01651646 195 inode = VFS_I(ip);
979ebab1
CH
196
197 error = xfs_init_security(inode, dir);
db0bb7ba
CH
198 if (unlikely(error))
199 goto out_cleanup_inode;
200
201 if (default_acl) {
ef14f0c1 202 error = -xfs_inherit_acl(inode, default_acl);
db0bb7ba
CH
203 if (unlikely(error))
204 goto out_cleanup_inode;
ef14f0c1 205 posix_acl_release(default_acl);
1da177e4
LT
206 }
207
1da177e4 208
db0bb7ba 209 d_instantiate(dentry, inode);
db0bb7ba
CH
210 return -error;
211
212 out_cleanup_inode:
8f112e3b 213 xfs_cleanup_inode(dir, inode, dentry);
db0bb7ba 214 out_free_acl:
ef14f0c1 215 posix_acl_release(default_acl);
1da177e4
LT
216 return -error;
217}
218
219STATIC int
416c6d5b 220xfs_vn_create(
1da177e4
LT
221 struct inode *dir,
222 struct dentry *dentry,
223 int mode,
224 struct nameidata *nd)
225{
416c6d5b 226 return xfs_vn_mknod(dir, dentry, mode, 0);
1da177e4
LT
227}
228
229STATIC int
416c6d5b 230xfs_vn_mkdir(
1da177e4
LT
231 struct inode *dir,
232 struct dentry *dentry,
233 int mode)
234{
416c6d5b 235 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
1da177e4
LT
236}
237
238STATIC struct dentry *
416c6d5b 239xfs_vn_lookup(
1da177e4
LT
240 struct inode *dir,
241 struct dentry *dentry,
242 struct nameidata *nd)
243{
ef1f5e7a 244 struct xfs_inode *cip;
556b8b16 245 struct xfs_name name;
1da177e4
LT
246 int error;
247
248 if (dentry->d_name.len >= MAXNAMELEN)
249 return ERR_PTR(-ENAMETOOLONG);
250
556b8b16 251 xfs_dentry_to_name(&name, dentry);
384f3ced 252 error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
67fcaa73 253 if (unlikely(error)) {
1da177e4
LT
254 if (unlikely(error != ENOENT))
255 return ERR_PTR(-error);
256 d_add(dentry, NULL);
257 return NULL;
258 }
259
01651646 260 return d_splice_alias(VFS_I(cip), dentry);
1da177e4
LT
261}
262
384f3ced
BN
263STATIC struct dentry *
264xfs_vn_ci_lookup(
265 struct inode *dir,
266 struct dentry *dentry,
267 struct nameidata *nd)
268{
269 struct xfs_inode *ip;
270 struct xfs_name xname;
271 struct xfs_name ci_name;
272 struct qstr dname;
273 int error;
274
275 if (dentry->d_name.len >= MAXNAMELEN)
276 return ERR_PTR(-ENAMETOOLONG);
277
278 xfs_dentry_to_name(&xname, dentry);
279 error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
280 if (unlikely(error)) {
281 if (unlikely(error != ENOENT))
282 return ERR_PTR(-error);
866d5dc9
BN
283 /*
284 * call d_add(dentry, NULL) here when d_drop_negative_children
285 * is called in xfs_vn_mknod (ie. allow negative dentries
286 * with CI filesystems).
287 */
384f3ced
BN
288 return NULL;
289 }
290
291 /* if exact match, just splice and exit */
292 if (!ci_name.name)
01651646 293 return d_splice_alias(VFS_I(ip), dentry);
384f3ced
BN
294
295 /* else case-insensitive match... */
296 dname.name = ci_name.name;
297 dname.len = ci_name.len;
e45b590b 298 dentry = d_add_ci(dentry, VFS_I(ip), &dname);
384f3ced
BN
299 kmem_free(ci_name.name);
300 return dentry;
301}
302
1da177e4 303STATIC int
416c6d5b 304xfs_vn_link(
1da177e4
LT
305 struct dentry *old_dentry,
306 struct inode *dir,
307 struct dentry *dentry)
308{
d9424b3c 309 struct inode *inode = old_dentry->d_inode;
556b8b16 310 struct xfs_name name;
1da177e4
LT
311 int error;
312
556b8b16 313 xfs_dentry_to_name(&name, dentry);
1da177e4 314
556b8b16 315 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
d9424b3c 316 if (unlikely(error))
a3da7896 317 return -error;
a3da7896 318
7de9c6ee 319 ihold(inode);
a3da7896
CH
320 d_instantiate(dentry, inode);
321 return 0;
1da177e4
LT
322}
323
324STATIC int
416c6d5b 325xfs_vn_unlink(
1da177e4
LT
326 struct inode *dir,
327 struct dentry *dentry)
328{
556b8b16 329 struct xfs_name name;
1da177e4
LT
330 int error;
331
556b8b16 332 xfs_dentry_to_name(&name, dentry);
1da177e4 333
e5700704
CH
334 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
335 if (error)
336 return error;
337
338 /*
339 * With unlink, the VFS makes the dentry "negative": no inode,
340 * but still hashed. This is incompatible with case-insensitive
341 * mode, so invalidate (unhash) the dentry in CI-mode.
342 */
343 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
344 d_invalidate(dentry);
345 return 0;
1da177e4
LT
346}
347
348STATIC int
416c6d5b 349xfs_vn_symlink(
1da177e4
LT
350 struct inode *dir,
351 struct dentry *dentry,
352 const char *symname)
353{
3937be5b
CH
354 struct inode *inode;
355 struct xfs_inode *cip = NULL;
556b8b16 356 struct xfs_name name;
1da177e4 357 int error;
3e5daf05 358 mode_t mode;
1da177e4 359
3e5daf05 360 mode = S_IFLNK |
ce3b0f8d 361 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
556b8b16 362 xfs_dentry_to_name(&name, dentry);
1da177e4 363
6c77b0ea 364 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
3937be5b
CH
365 if (unlikely(error))
366 goto out;
367
01651646 368 inode = VFS_I(cip);
3937be5b
CH
369
370 error = xfs_init_security(inode, dir);
371 if (unlikely(error))
372 goto out_cleanup_inode;
373
374 d_instantiate(dentry, inode);
3937be5b
CH
375 return 0;
376
377 out_cleanup_inode:
8f112e3b 378 xfs_cleanup_inode(dir, inode, dentry);
3937be5b 379 out:
1da177e4
LT
380 return -error;
381}
382
1da177e4 383STATIC int
416c6d5b 384xfs_vn_rename(
1da177e4
LT
385 struct inode *odir,
386 struct dentry *odentry,
387 struct inode *ndir,
388 struct dentry *ndentry)
389{
390 struct inode *new_inode = ndentry->d_inode;
556b8b16
BN
391 struct xfs_name oname;
392 struct xfs_name nname;
1da177e4 393
556b8b16
BN
394 xfs_dentry_to_name(&oname, odentry);
395 xfs_dentry_to_name(&nname, ndentry);
396
e5700704 397 return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
cfa853e4
CH
398 XFS_I(ndir), &nname, new_inode ?
399 XFS_I(new_inode) : NULL);
1da177e4
LT
400}
401
402/*
403 * careful here - this function can get called recursively, so
404 * we need to be very careful about how much stack we use.
405 * uio is kmalloced for this reason...
406 */
008b150a 407STATIC void *
416c6d5b 408xfs_vn_follow_link(
1da177e4
LT
409 struct dentry *dentry,
410 struct nameidata *nd)
411{
1da177e4 412 char *link;
804c83c3 413 int error = -ENOMEM;
1da177e4 414
f52720ca 415 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
804c83c3
CH
416 if (!link)
417 goto out_err;
1da177e4 418
739bfb2a 419 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
804c83c3
CH
420 if (unlikely(error))
421 goto out_kfree;
1da177e4
LT
422
423 nd_set_link(nd, link);
008b150a 424 return NULL;
804c83c3
CH
425
426 out_kfree:
427 kfree(link);
428 out_err:
429 nd_set_link(nd, ERR_PTR(error));
430 return NULL;
1da177e4
LT
431}
432
cde410a9 433STATIC void
416c6d5b 434xfs_vn_put_link(
cde410a9
NS
435 struct dentry *dentry,
436 struct nameidata *nd,
437 void *p)
1da177e4 438{
cde410a9
NS
439 char *s = nd_get_link(nd);
440
1da177e4
LT
441 if (!IS_ERR(s))
442 kfree(s);
443}
444
1da177e4 445STATIC int
416c6d5b 446xfs_vn_getattr(
c43f4087
CH
447 struct vfsmount *mnt,
448 struct dentry *dentry,
449 struct kstat *stat)
1da177e4 450{
c43f4087
CH
451 struct inode *inode = dentry->d_inode;
452 struct xfs_inode *ip = XFS_I(inode);
453 struct xfs_mount *mp = ip->i_mount;
454
cca28fb8 455 trace_xfs_getattr(ip);
c43f4087
CH
456
457 if (XFS_FORCED_SHUTDOWN(mp))
458 return XFS_ERROR(EIO);
459
460 stat->size = XFS_ISIZE(ip);
461 stat->dev = inode->i_sb->s_dev;
462 stat->mode = ip->i_d.di_mode;
463 stat->nlink = ip->i_d.di_nlink;
464 stat->uid = ip->i_d.di_uid;
465 stat->gid = ip->i_d.di_gid;
466 stat->ino = ip->i_ino;
c43f4087 467 stat->atime = inode->i_atime;
f9581b14
CH
468 stat->mtime = inode->i_mtime;
469 stat->ctime = inode->i_ctime;
c43f4087
CH
470 stat->blocks =
471 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
472
473
474 switch (inode->i_mode & S_IFMT) {
475 case S_IFBLK:
476 case S_IFCHR:
477 stat->blksize = BLKDEV_IOSIZE;
478 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
479 sysv_minor(ip->i_df.if_u2.if_rdev));
480 break;
481 default:
71ddabb9 482 if (XFS_IS_REALTIME_INODE(ip)) {
c43f4087
CH
483 /*
484 * If the file blocks are being allocated from a
485 * realtime volume, then return the inode's realtime
486 * extent size or the realtime volume's extent size.
487 */
488 stat->blksize =
489 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
490 } else
491 stat->blksize = xfs_preferred_iosize(mp);
492 stat->rdev = 0;
493 break;
69e23b9a 494 }
c43f4087
CH
495
496 return 0;
1da177e4
LT
497}
498
499STATIC int
416c6d5b 500xfs_vn_setattr(
1da177e4 501 struct dentry *dentry,
0f285c8a 502 struct iattr *iattr)
1da177e4 503{
ea5a3dc8 504 return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0);
1da177e4
LT
505}
506
f35642e2
ES
507#define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
508
509/*
510 * Call fiemap helper to fill in user data.
511 * Returns positive errors to xfs_getbmap.
512 */
513STATIC int
514xfs_fiemap_format(
515 void **arg,
516 struct getbmapx *bmv,
517 int *full)
518{
519 int error;
520 struct fiemap_extent_info *fieinfo = *arg;
521 u32 fiemap_flags = 0;
522 u64 logical, physical, length;
523
524 /* Do nothing for a hole */
525 if (bmv->bmv_block == -1LL)
526 return 0;
527
528 logical = BBTOB(bmv->bmv_offset);
529 physical = BBTOB(bmv->bmv_block);
530 length = BBTOB(bmv->bmv_length);
531
532 if (bmv->bmv_oflags & BMV_OF_PREALLOC)
533 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
534 else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
535 fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
536 physical = 0; /* no block yet */
537 }
538 if (bmv->bmv_oflags & BMV_OF_LAST)
539 fiemap_flags |= FIEMAP_EXTENT_LAST;
540
541 error = fiemap_fill_next_extent(fieinfo, logical, physical,
542 length, fiemap_flags);
543 if (error > 0) {
544 error = 0;
545 *full = 1; /* user array now full */
546 }
547
548 return -error;
549}
550
551STATIC int
552xfs_vn_fiemap(
553 struct inode *inode,
554 struct fiemap_extent_info *fieinfo,
555 u64 start,
556 u64 length)
557{
558 xfs_inode_t *ip = XFS_I(inode);
559 struct getbmapx bm;
560 int error;
561
562 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
563 if (error)
564 return error;
565
566 /* Set up bmap header for xfs internal routine */
567 bm.bmv_offset = BTOBB(start);
568 /* Special case for whole file */
569 if (length == FIEMAP_MAX_OFFSET)
570 bm.bmv_length = -1LL;
571 else
572 bm.bmv_length = BTOBB(length);
573
97db39a1 574 /* We add one because in getbmap world count includes the header */
2d1ff3c7
TM
575 bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
576 fieinfo->fi_extents_max + 1;
577 bm.bmv_count = min_t(__s32, bm.bmv_count,
578 (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
9af25465 579 bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
f35642e2
ES
580 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
581 bm.bmv_iflags |= BMV_IF_ATTRFORK;
582 if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
583 bm.bmv_iflags |= BMV_IF_DELALLOC;
584
585 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
586 if (error)
587 return -error;
588
589 return 0;
590}
591
41be8bed 592static const struct inode_operations xfs_inode_operations = {
18f4c644 593 .check_acl = xfs_check_acl,
416c6d5b
NS
594 .getattr = xfs_vn_getattr,
595 .setattr = xfs_vn_setattr,
0ec58516
LM
596 .setxattr = generic_setxattr,
597 .getxattr = generic_getxattr,
598 .removexattr = generic_removexattr,
416c6d5b 599 .listxattr = xfs_vn_listxattr,
f35642e2 600 .fiemap = xfs_vn_fiemap,
1da177e4
LT
601};
602
41be8bed 603static const struct inode_operations xfs_dir_inode_operations = {
416c6d5b
NS
604 .create = xfs_vn_create,
605 .lookup = xfs_vn_lookup,
606 .link = xfs_vn_link,
607 .unlink = xfs_vn_unlink,
608 .symlink = xfs_vn_symlink,
609 .mkdir = xfs_vn_mkdir,
8f112e3b
CH
610 /*
611 * Yes, XFS uses the same method for rmdir and unlink.
612 *
613 * There are some subtile differences deeper in the code,
614 * but we use S_ISDIR to check for those.
615 */
616 .rmdir = xfs_vn_unlink,
416c6d5b
NS
617 .mknod = xfs_vn_mknod,
618 .rename = xfs_vn_rename,
18f4c644 619 .check_acl = xfs_check_acl,
416c6d5b
NS
620 .getattr = xfs_vn_getattr,
621 .setattr = xfs_vn_setattr,
0ec58516
LM
622 .setxattr = generic_setxattr,
623 .getxattr = generic_getxattr,
624 .removexattr = generic_removexattr,
416c6d5b 625 .listxattr = xfs_vn_listxattr,
1da177e4
LT
626};
627
41be8bed 628static const struct inode_operations xfs_dir_ci_inode_operations = {
384f3ced
BN
629 .create = xfs_vn_create,
630 .lookup = xfs_vn_ci_lookup,
631 .link = xfs_vn_link,
632 .unlink = xfs_vn_unlink,
633 .symlink = xfs_vn_symlink,
634 .mkdir = xfs_vn_mkdir,
8f112e3b
CH
635 /*
636 * Yes, XFS uses the same method for rmdir and unlink.
637 *
638 * There are some subtile differences deeper in the code,
639 * but we use S_ISDIR to check for those.
640 */
641 .rmdir = xfs_vn_unlink,
384f3ced
BN
642 .mknod = xfs_vn_mknod,
643 .rename = xfs_vn_rename,
18f4c644 644 .check_acl = xfs_check_acl,
384f3ced
BN
645 .getattr = xfs_vn_getattr,
646 .setattr = xfs_vn_setattr,
0ec58516
LM
647 .setxattr = generic_setxattr,
648 .getxattr = generic_getxattr,
649 .removexattr = generic_removexattr,
384f3ced 650 .listxattr = xfs_vn_listxattr,
384f3ced
BN
651};
652
41be8bed 653static const struct inode_operations xfs_symlink_inode_operations = {
1da177e4 654 .readlink = generic_readlink,
416c6d5b
NS
655 .follow_link = xfs_vn_follow_link,
656 .put_link = xfs_vn_put_link,
18f4c644 657 .check_acl = xfs_check_acl,
416c6d5b
NS
658 .getattr = xfs_vn_getattr,
659 .setattr = xfs_vn_setattr,
0ec58516
LM
660 .setxattr = generic_setxattr,
661 .getxattr = generic_getxattr,
662 .removexattr = generic_removexattr,
416c6d5b 663 .listxattr = xfs_vn_listxattr,
1da177e4 664};
41be8bed
CH
665
666STATIC void
667xfs_diflags_to_iflags(
668 struct inode *inode,
669 struct xfs_inode *ip)
670{
671 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
672 inode->i_flags |= S_IMMUTABLE;
673 else
674 inode->i_flags &= ~S_IMMUTABLE;
675 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
676 inode->i_flags |= S_APPEND;
677 else
678 inode->i_flags &= ~S_APPEND;
679 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
680 inode->i_flags |= S_SYNC;
681 else
682 inode->i_flags &= ~S_SYNC;
683 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
684 inode->i_flags |= S_NOATIME;
685 else
686 inode->i_flags &= ~S_NOATIME;
687}
688
689/*
690 * Initialize the Linux inode, set up the operation vectors and
691 * unlock the inode.
692 *
693 * When reading existing inodes from disk this is called directly
694 * from xfs_iget, when creating a new inode it is called from
695 * xfs_ialloc after setting up the inode.
bf904248
DC
696 *
697 * We are always called with an uninitialised linux inode here.
698 * We need to initialise the necessary fields and take a reference
699 * on it.
41be8bed
CH
700 */
701void
702xfs_setup_inode(
703 struct xfs_inode *ip)
704{
bf904248
DC
705 struct inode *inode = &ip->i_vnode;
706
707 inode->i_ino = ip->i_ino;
eaff8079 708 inode->i_state = I_NEW;
646ec461
CH
709
710 inode_sb_list_add(inode);
c6f6cd06
CH
711 /* make the inode look hashed for the writeback code */
712 hlist_add_fake(&inode->i_hash);
41be8bed
CH
713
714 inode->i_mode = ip->i_d.di_mode;
715 inode->i_nlink = ip->i_d.di_nlink;
716 inode->i_uid = ip->i_d.di_uid;
717 inode->i_gid = ip->i_d.di_gid;
718
719 switch (inode->i_mode & S_IFMT) {
720 case S_IFBLK:
721 case S_IFCHR:
722 inode->i_rdev =
723 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
724 sysv_minor(ip->i_df.if_u2.if_rdev));
725 break;
726 default:
727 inode->i_rdev = 0;
728 break;
729 }
730
731 inode->i_generation = ip->i_d.di_gen;
732 i_size_write(inode, ip->i_d.di_size);
733 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
734 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
735 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
736 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
737 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
738 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
739 xfs_diflags_to_iflags(inode, ip);
41be8bed
CH
740
741 switch (inode->i_mode & S_IFMT) {
742 case S_IFREG:
743 inode->i_op = &xfs_inode_operations;
744 inode->i_fop = &xfs_file_operations;
745 inode->i_mapping->a_ops = &xfs_address_space_operations;
746 break;
747 case S_IFDIR:
748 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
749 inode->i_op = &xfs_dir_ci_inode_operations;
750 else
751 inode->i_op = &xfs_dir_inode_operations;
752 inode->i_fop = &xfs_dir_file_operations;
753 break;
754 case S_IFLNK:
755 inode->i_op = &xfs_symlink_inode_operations;
756 if (!(ip->i_df.if_flags & XFS_IFINLINE))
757 inode->i_mapping->a_ops = &xfs_address_space_operations;
758 break;
759 default:
760 inode->i_op = &xfs_inode_operations;
761 init_special_inode(inode, inode->i_mode, inode->i_rdev);
762 break;
763 }
764
765 xfs_iflags_clear(ip, XFS_INEW);
766 barrier();
767
768 unlock_new_inode(inode);
769}