xfs_iops.c 20 KB

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