xfs_iops.c 19 KB

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