xfs_iops.c 20 KB

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