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