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