xfs_iops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. bhv_vnode_t *vp;
  64. vp = XFS_ITOV_NULL(ip);
  65. if (vp) {
  66. ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
  67. ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
  68. }
  69. }
  70. /*
  71. * If the linux inode exists, mark it dirty.
  72. * Used when commiting a dirty inode into a transaction so that
  73. * the inode will get written back by the linux code
  74. */
  75. void
  76. xfs_mark_inode_dirty_sync(
  77. xfs_inode_t *ip)
  78. {
  79. bhv_vnode_t *vp;
  80. vp = XFS_ITOV_NULL(ip);
  81. if (vp)
  82. mark_inode_dirty_sync(vn_to_inode(vp));
  83. }
  84. /*
  85. * Change the requested timestamp in the given inode.
  86. * We don't lock across timestamp updates, and we don't log them but
  87. * we do record the fact that there is dirty information in core.
  88. *
  89. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  90. * with XFS_ICHGTIME_ACC to be sure that access time
  91. * update will take. Calling first with XFS_ICHGTIME_ACC
  92. * and then XFS_ICHGTIME_MOD may fail to modify the access
  93. * timestamp if the filesystem is mounted noacctm.
  94. */
  95. void
  96. xfs_ichgtime(
  97. xfs_inode_t *ip,
  98. int flags)
  99. {
  100. struct inode *inode = vn_to_inode(XFS_ITOV(ip));
  101. timespec_t tv;
  102. nanotime(&tv);
  103. if (flags & XFS_ICHGTIME_MOD) {
  104. inode->i_mtime = tv;
  105. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  106. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  107. }
  108. if (flags & XFS_ICHGTIME_ACC) {
  109. inode->i_atime = tv;
  110. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  111. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  112. }
  113. if (flags & XFS_ICHGTIME_CHG) {
  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. }
  118. /*
  119. * We update the i_update_core field _after_ changing
  120. * the timestamps in order to coordinate properly with
  121. * xfs_iflush() so that we don't lose timestamp updates.
  122. * This keeps us from having to hold the inode lock
  123. * while doing this. We use the SYNCHRONIZE macro to
  124. * ensure that the compiler does not reorder the update
  125. * of i_update_core above the timestamp updates above.
  126. */
  127. SYNCHRONIZE();
  128. ip->i_update_core = 1;
  129. if (!(inode->i_state & I_NEW))
  130. mark_inode_dirty_sync(inode);
  131. }
  132. /*
  133. * Variant on the above which avoids querying the system clock
  134. * in situations where we know the Linux inode timestamps have
  135. * just been updated (and so we can update our inode cheaply).
  136. */
  137. void
  138. xfs_ichgtime_fast(
  139. xfs_inode_t *ip,
  140. struct inode *inode,
  141. int flags)
  142. {
  143. timespec_t *tvp;
  144. /*
  145. * Atime updates for read() & friends are handled lazily now, and
  146. * explicit updates must go through xfs_ichgtime()
  147. */
  148. ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
  149. /*
  150. * We're not supposed to change timestamps in readonly-mounted
  151. * filesystems. Throw it away if anyone asks us.
  152. */
  153. if (unlikely(IS_RDONLY(inode)))
  154. return;
  155. if (flags & XFS_ICHGTIME_MOD) {
  156. tvp = &inode->i_mtime;
  157. ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
  158. ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
  159. }
  160. if (flags & XFS_ICHGTIME_CHG) {
  161. tvp = &inode->i_ctime;
  162. ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
  163. ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
  164. }
  165. /*
  166. * We update the i_update_core field _after_ changing
  167. * the timestamps in order to coordinate properly with
  168. * xfs_iflush() so that we don't lose timestamp updates.
  169. * This keeps us from having to hold the inode lock
  170. * while doing this. We use the SYNCHRONIZE macro to
  171. * ensure that the compiler does not reorder the update
  172. * of i_update_core above the timestamp updates above.
  173. */
  174. SYNCHRONIZE();
  175. ip->i_update_core = 1;
  176. if (!(inode->i_state & I_NEW))
  177. mark_inode_dirty_sync(inode);
  178. }
  179. /*
  180. * Pull the link count and size up from the xfs inode to the linux inode
  181. */
  182. STATIC void
  183. xfs_validate_fields(
  184. struct inode *inode)
  185. {
  186. struct xfs_inode *ip = XFS_I(inode);
  187. loff_t size;
  188. inode->i_nlink = ip->i_d.di_nlink;
  189. inode->i_blocks =
  190. XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks +
  191. ip->i_delayed_blks);
  192. /* we're under i_sem so i_size can't change under us */
  193. size = XFS_ISIZE(ip);
  194. if (i_size_read(inode) != size)
  195. i_size_write(inode, size);
  196. }
  197. /*
  198. * Hook in SELinux. This is not quite correct yet, what we really need
  199. * here (as we do for default ACLs) is a mechanism by which creation of
  200. * these attrs can be journalled at inode creation time (along with the
  201. * inode, of course, such that log replay can't cause these to be lost).
  202. */
  203. STATIC int
  204. xfs_init_security(
  205. bhv_vnode_t *vp,
  206. struct inode *dir)
  207. {
  208. struct inode *ip = vn_to_inode(vp);
  209. size_t length;
  210. void *value;
  211. char *name;
  212. int error;
  213. error = security_inode_init_security(ip, dir, &name, &value, &length);
  214. if (error) {
  215. if (error == -EOPNOTSUPP)
  216. return 0;
  217. return -error;
  218. }
  219. error = xfs_attr_set(XFS_I(ip), name, value,
  220. length, ATTR_SECURE);
  221. if (!error)
  222. xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
  223. kfree(name);
  224. kfree(value);
  225. return error;
  226. }
  227. /*
  228. * Determine whether a process has a valid fs_struct (kernel daemons
  229. * like knfsd don't have an fs_struct).
  230. *
  231. * XXX(hch): nfsd is broken, better fix it instead.
  232. */
  233. STATIC_INLINE int
  234. xfs_has_fs_struct(struct task_struct *task)
  235. {
  236. return (task->fs != init_task.fs);
  237. }
  238. STATIC void
  239. xfs_cleanup_inode(
  240. struct inode *dir,
  241. bhv_vnode_t *vp,
  242. struct dentry *dentry,
  243. int mode)
  244. {
  245. struct dentry teardown = {};
  246. /* Oh, the horror.
  247. * If we can't add the ACL or we fail in
  248. * xfs_init_security we must back out.
  249. * ENOSPC can hit here, among other things.
  250. */
  251. teardown.d_inode = vn_to_inode(vp);
  252. teardown.d_name = dentry->d_name;
  253. if (S_ISDIR(mode))
  254. xfs_rmdir(XFS_I(dir), &teardown);
  255. else
  256. xfs_remove(XFS_I(dir), &teardown);
  257. VN_RELE(vp);
  258. }
  259. STATIC int
  260. xfs_vn_mknod(
  261. struct inode *dir,
  262. struct dentry *dentry,
  263. int mode,
  264. dev_t rdev)
  265. {
  266. struct inode *ip;
  267. bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
  268. xfs_acl_t *default_acl = NULL;
  269. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  270. int error;
  271. /*
  272. * Irix uses Missed'em'V split, but doesn't want to see
  273. * the upper 5 bits of (14bit) major.
  274. */
  275. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  276. return -EINVAL;
  277. if (unlikely(test_default_acl && test_default_acl(dvp))) {
  278. if (!_ACL_ALLOC(default_acl)) {
  279. return -ENOMEM;
  280. }
  281. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  282. _ACL_FREE(default_acl);
  283. default_acl = NULL;
  284. }
  285. }
  286. if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
  287. mode &= ~current->fs->umask;
  288. switch (mode & S_IFMT) {
  289. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  290. rdev = sysv_encode_dev(rdev);
  291. case S_IFREG:
  292. error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
  293. break;
  294. case S_IFDIR:
  295. error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
  296. break;
  297. default:
  298. error = EINVAL;
  299. break;
  300. }
  301. if (unlikely(!error)) {
  302. error = xfs_init_security(vp, dir);
  303. if (error)
  304. xfs_cleanup_inode(dir, vp, dentry, mode);
  305. }
  306. if (unlikely(default_acl)) {
  307. if (!error) {
  308. error = _ACL_INHERIT(vp, mode, default_acl);
  309. if (!error)
  310. xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
  311. else
  312. xfs_cleanup_inode(dir, vp, dentry, mode);
  313. }
  314. _ACL_FREE(default_acl);
  315. }
  316. if (likely(!error)) {
  317. ASSERT(vp);
  318. ip = vn_to_inode(vp);
  319. if (S_ISDIR(mode))
  320. xfs_validate_fields(ip);
  321. d_instantiate(dentry, ip);
  322. xfs_validate_fields(dir);
  323. }
  324. return -error;
  325. }
  326. STATIC int
  327. xfs_vn_create(
  328. struct inode *dir,
  329. struct dentry *dentry,
  330. int mode,
  331. struct nameidata *nd)
  332. {
  333. return xfs_vn_mknod(dir, dentry, mode, 0);
  334. }
  335. STATIC int
  336. xfs_vn_mkdir(
  337. struct inode *dir,
  338. struct dentry *dentry,
  339. int mode)
  340. {
  341. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  342. }
  343. STATIC struct dentry *
  344. xfs_vn_lookup(
  345. struct inode *dir,
  346. struct dentry *dentry,
  347. struct nameidata *nd)
  348. {
  349. bhv_vnode_t *cvp;
  350. int error;
  351. if (dentry->d_name.len >= MAXNAMELEN)
  352. return ERR_PTR(-ENAMETOOLONG);
  353. error = xfs_lookup(XFS_I(dir), dentry, &cvp);
  354. if (unlikely(error)) {
  355. if (unlikely(error != ENOENT))
  356. return ERR_PTR(-error);
  357. d_add(dentry, NULL);
  358. return NULL;
  359. }
  360. return d_splice_alias(vn_to_inode(cvp), dentry);
  361. }
  362. STATIC int
  363. xfs_vn_link(
  364. struct dentry *old_dentry,
  365. struct inode *dir,
  366. struct dentry *dentry)
  367. {
  368. struct inode *ip; /* inode of guy being linked to */
  369. bhv_vnode_t *vp; /* vp of name being linked */
  370. int error;
  371. ip = old_dentry->d_inode; /* inode being linked to */
  372. vp = vn_from_inode(ip);
  373. VN_HOLD(vp);
  374. error = xfs_link(XFS_I(dir), vp, dentry);
  375. if (unlikely(error)) {
  376. VN_RELE(vp);
  377. } else {
  378. xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
  379. xfs_validate_fields(ip);
  380. d_instantiate(dentry, ip);
  381. }
  382. return -error;
  383. }
  384. STATIC int
  385. xfs_vn_unlink(
  386. struct inode *dir,
  387. struct dentry *dentry)
  388. {
  389. struct inode *inode;
  390. int error;
  391. inode = dentry->d_inode;
  392. error = xfs_remove(XFS_I(dir), dentry);
  393. if (likely(!error)) {
  394. xfs_validate_fields(dir); /* size needs update */
  395. xfs_validate_fields(inode);
  396. }
  397. return -error;
  398. }
  399. STATIC int
  400. xfs_vn_symlink(
  401. struct inode *dir,
  402. struct dentry *dentry,
  403. const char *symname)
  404. {
  405. struct inode *ip;
  406. bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
  407. int error;
  408. mode_t mode;
  409. cvp = NULL;
  410. mode = S_IFLNK |
  411. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  412. error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
  413. &cvp, NULL);
  414. if (likely(!error && cvp)) {
  415. error = xfs_init_security(cvp, dir);
  416. if (likely(!error)) {
  417. ip = vn_to_inode(cvp);
  418. d_instantiate(dentry, ip);
  419. xfs_validate_fields(dir);
  420. xfs_validate_fields(ip);
  421. } else {
  422. xfs_cleanup_inode(dir, cvp, dentry, 0);
  423. }
  424. }
  425. return -error;
  426. }
  427. STATIC int
  428. xfs_vn_rmdir(
  429. struct inode *dir,
  430. struct dentry *dentry)
  431. {
  432. struct inode *inode = dentry->d_inode;
  433. int error;
  434. error = xfs_rmdir(XFS_I(dir), dentry);
  435. if (likely(!error)) {
  436. xfs_validate_fields(inode);
  437. xfs_validate_fields(dir);
  438. }
  439. return -error;
  440. }
  441. STATIC int
  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. bhv_vnode_t *tvp; /* target directory */
  450. int error;
  451. tvp = vn_from_inode(ndir);
  452. error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
  453. if (likely(!error)) {
  454. if (new_inode)
  455. xfs_validate_fields(new_inode);
  456. xfs_validate_fields(odir);
  457. if (ndir != odir)
  458. xfs_validate_fields(ndir);
  459. }
  460. return -error;
  461. }
  462. /*
  463. * careful here - this function can get called recursively, so
  464. * we need to be very careful about how much stack we use.
  465. * uio is kmalloced for this reason...
  466. */
  467. STATIC void *
  468. xfs_vn_follow_link(
  469. struct dentry *dentry,
  470. struct nameidata *nd)
  471. {
  472. char *link;
  473. int error = -ENOMEM;
  474. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  475. if (!link)
  476. goto out_err;
  477. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  478. if (unlikely(error))
  479. goto out_kfree;
  480. nd_set_link(nd, link);
  481. return NULL;
  482. out_kfree:
  483. kfree(link);
  484. out_err:
  485. nd_set_link(nd, ERR_PTR(error));
  486. return NULL;
  487. }
  488. STATIC void
  489. xfs_vn_put_link(
  490. struct dentry *dentry,
  491. struct nameidata *nd,
  492. void *p)
  493. {
  494. char *s = nd_get_link(nd);
  495. if (!IS_ERR(s))
  496. kfree(s);
  497. }
  498. #ifdef CONFIG_XFS_POSIX_ACL
  499. STATIC int
  500. xfs_check_acl(
  501. struct inode *inode,
  502. int mask)
  503. {
  504. struct xfs_inode *ip = XFS_I(inode);
  505. int error;
  506. xfs_itrace_entry(ip);
  507. if (XFS_IFORK_Q(ip)) {
  508. error = xfs_acl_iaccess(ip, mask, NULL);
  509. if (error != -1)
  510. return -error;
  511. }
  512. return -EAGAIN;
  513. }
  514. STATIC int
  515. xfs_vn_permission(
  516. struct inode *inode,
  517. int mask,
  518. struct nameidata *nd)
  519. {
  520. return generic_permission(inode, mask, xfs_check_acl);
  521. }
  522. #else
  523. #define xfs_vn_permission NULL
  524. #endif
  525. STATIC int
  526. xfs_vn_getattr(
  527. struct vfsmount *mnt,
  528. struct dentry *dentry,
  529. struct kstat *stat)
  530. {
  531. struct inode *inode = dentry->d_inode;
  532. struct xfs_inode *ip = XFS_I(inode);
  533. struct xfs_mount *mp = ip->i_mount;
  534. xfs_itrace_entry(ip);
  535. if (XFS_FORCED_SHUTDOWN(mp))
  536. return XFS_ERROR(EIO);
  537. stat->size = XFS_ISIZE(ip);
  538. stat->dev = inode->i_sb->s_dev;
  539. stat->mode = ip->i_d.di_mode;
  540. stat->nlink = ip->i_d.di_nlink;
  541. stat->uid = ip->i_d.di_uid;
  542. stat->gid = ip->i_d.di_gid;
  543. stat->ino = ip->i_ino;
  544. #if XFS_BIG_INUMS
  545. stat->ino += mp->m_inoadd;
  546. #endif
  547. stat->atime = inode->i_atime;
  548. stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  549. stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  550. stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  551. stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  552. stat->blocks =
  553. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  554. switch (inode->i_mode & S_IFMT) {
  555. case S_IFBLK:
  556. case S_IFCHR:
  557. stat->blksize = BLKDEV_IOSIZE;
  558. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  559. sysv_minor(ip->i_df.if_u2.if_rdev));
  560. break;
  561. default:
  562. if (XFS_IS_REALTIME_INODE(ip)) {
  563. /*
  564. * If the file blocks are being allocated from a
  565. * realtime volume, then return the inode's realtime
  566. * extent size or the realtime volume's extent size.
  567. */
  568. stat->blksize =
  569. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  570. } else
  571. stat->blksize = xfs_preferred_iosize(mp);
  572. stat->rdev = 0;
  573. break;
  574. }
  575. return 0;
  576. }
  577. STATIC int
  578. xfs_vn_setattr(
  579. struct dentry *dentry,
  580. struct iattr *attr)
  581. {
  582. struct inode *inode = dentry->d_inode;
  583. unsigned int ia_valid = attr->ia_valid;
  584. bhv_vattr_t vattr = { 0 };
  585. int flags = 0;
  586. int error;
  587. if (ia_valid & ATTR_UID) {
  588. vattr.va_mask |= XFS_AT_UID;
  589. vattr.va_uid = attr->ia_uid;
  590. }
  591. if (ia_valid & ATTR_GID) {
  592. vattr.va_mask |= XFS_AT_GID;
  593. vattr.va_gid = attr->ia_gid;
  594. }
  595. if (ia_valid & ATTR_SIZE) {
  596. vattr.va_mask |= XFS_AT_SIZE;
  597. vattr.va_size = attr->ia_size;
  598. }
  599. if (ia_valid & ATTR_ATIME) {
  600. vattr.va_mask |= XFS_AT_ATIME;
  601. vattr.va_atime = attr->ia_atime;
  602. inode->i_atime = attr->ia_atime;
  603. }
  604. if (ia_valid & ATTR_MTIME) {
  605. vattr.va_mask |= XFS_AT_MTIME;
  606. vattr.va_mtime = attr->ia_mtime;
  607. }
  608. if (ia_valid & ATTR_CTIME) {
  609. vattr.va_mask |= XFS_AT_CTIME;
  610. vattr.va_ctime = attr->ia_ctime;
  611. }
  612. if (ia_valid & ATTR_MODE) {
  613. vattr.va_mask |= XFS_AT_MODE;
  614. vattr.va_mode = attr->ia_mode;
  615. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  616. inode->i_mode &= ~S_ISGID;
  617. }
  618. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  619. flags |= ATTR_UTIME;
  620. #ifdef ATTR_NO_BLOCK
  621. if ((ia_valid & ATTR_NO_BLOCK))
  622. flags |= ATTR_NONBLOCK;
  623. #endif
  624. error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
  625. if (likely(!error))
  626. vn_revalidate(vn_from_inode(inode));
  627. return -error;
  628. }
  629. STATIC void
  630. xfs_vn_truncate(
  631. struct inode *inode)
  632. {
  633. block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
  634. }
  635. STATIC int
  636. xfs_vn_setxattr(
  637. struct dentry *dentry,
  638. const char *name,
  639. const void *data,
  640. size_t size,
  641. int flags)
  642. {
  643. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  644. char *attr = (char *)name;
  645. attrnames_t *namesp;
  646. int xflags = 0;
  647. int error;
  648. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  649. if (!namesp)
  650. return -EOPNOTSUPP;
  651. attr += namesp->attr_namelen;
  652. error = namesp->attr_capable(vp, NULL);
  653. if (error)
  654. return error;
  655. /* Convert Linux syscall to XFS internal ATTR flags */
  656. if (flags & XATTR_CREATE)
  657. xflags |= ATTR_CREATE;
  658. if (flags & XATTR_REPLACE)
  659. xflags |= ATTR_REPLACE;
  660. xflags |= namesp->attr_flag;
  661. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  662. }
  663. STATIC ssize_t
  664. xfs_vn_getxattr(
  665. struct dentry *dentry,
  666. const char *name,
  667. void *data,
  668. size_t size)
  669. {
  670. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  671. char *attr = (char *)name;
  672. attrnames_t *namesp;
  673. int xflags = 0;
  674. ssize_t error;
  675. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  676. if (!namesp)
  677. return -EOPNOTSUPP;
  678. attr += namesp->attr_namelen;
  679. error = namesp->attr_capable(vp, NULL);
  680. if (error)
  681. return error;
  682. /* Convert Linux syscall to XFS internal ATTR flags */
  683. if (!size) {
  684. xflags |= ATTR_KERNOVAL;
  685. data = NULL;
  686. }
  687. xflags |= namesp->attr_flag;
  688. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  689. }
  690. STATIC ssize_t
  691. xfs_vn_listxattr(
  692. struct dentry *dentry,
  693. char *data,
  694. size_t size)
  695. {
  696. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  697. int error, xflags = ATTR_KERNAMELS;
  698. ssize_t result;
  699. if (!size)
  700. xflags |= ATTR_KERNOVAL;
  701. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  702. error = attr_generic_list(vp, data, size, xflags, &result);
  703. if (error < 0)
  704. return error;
  705. return result;
  706. }
  707. STATIC int
  708. xfs_vn_removexattr(
  709. struct dentry *dentry,
  710. const char *name)
  711. {
  712. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  713. char *attr = (char *)name;
  714. attrnames_t *namesp;
  715. int xflags = 0;
  716. int error;
  717. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  718. if (!namesp)
  719. return -EOPNOTSUPP;
  720. attr += namesp->attr_namelen;
  721. error = namesp->attr_capable(vp, NULL);
  722. if (error)
  723. return error;
  724. xflags |= namesp->attr_flag;
  725. return namesp->attr_remove(vp, attr, xflags);
  726. }
  727. STATIC long
  728. xfs_vn_fallocate(
  729. struct inode *inode,
  730. int mode,
  731. loff_t offset,
  732. loff_t len)
  733. {
  734. long error;
  735. loff_t new_size = 0;
  736. xfs_flock64_t bf;
  737. xfs_inode_t *ip = XFS_I(inode);
  738. /* preallocation on directories not yet supported */
  739. error = -ENODEV;
  740. if (S_ISDIR(inode->i_mode))
  741. goto out_error;
  742. bf.l_whence = 0;
  743. bf.l_start = offset;
  744. bf.l_len = len;
  745. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  746. error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
  747. 0, NULL, ATTR_NOLOCK);
  748. if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
  749. offset + len > i_size_read(inode))
  750. new_size = offset + len;
  751. /* Change file size if needed */
  752. if (new_size) {
  753. bhv_vattr_t va;
  754. va.va_mask = XFS_AT_SIZE;
  755. va.va_size = new_size;
  756. error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
  757. }
  758. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  759. out_error:
  760. return error;
  761. }
  762. const struct inode_operations xfs_inode_operations = {
  763. .permission = xfs_vn_permission,
  764. .truncate = xfs_vn_truncate,
  765. .getattr = xfs_vn_getattr,
  766. .setattr = xfs_vn_setattr,
  767. .setxattr = xfs_vn_setxattr,
  768. .getxattr = xfs_vn_getxattr,
  769. .listxattr = xfs_vn_listxattr,
  770. .removexattr = xfs_vn_removexattr,
  771. .fallocate = xfs_vn_fallocate,
  772. };
  773. const struct inode_operations xfs_dir_inode_operations = {
  774. .create = xfs_vn_create,
  775. .lookup = xfs_vn_lookup,
  776. .link = xfs_vn_link,
  777. .unlink = xfs_vn_unlink,
  778. .symlink = xfs_vn_symlink,
  779. .mkdir = xfs_vn_mkdir,
  780. .rmdir = xfs_vn_rmdir,
  781. .mknod = xfs_vn_mknod,
  782. .rename = xfs_vn_rename,
  783. .permission = xfs_vn_permission,
  784. .getattr = xfs_vn_getattr,
  785. .setattr = xfs_vn_setattr,
  786. .setxattr = xfs_vn_setxattr,
  787. .getxattr = xfs_vn_getxattr,
  788. .listxattr = xfs_vn_listxattr,
  789. .removexattr = xfs_vn_removexattr,
  790. };
  791. const struct inode_operations xfs_symlink_inode_operations = {
  792. .readlink = generic_readlink,
  793. .follow_link = xfs_vn_follow_link,
  794. .put_link = xfs_vn_put_link,
  795. .permission = xfs_vn_permission,
  796. .getattr = xfs_vn_getattr,
  797. .setattr = xfs_vn_setattr,
  798. .setxattr = xfs_vn_setxattr,
  799. .getxattr = xfs_vn_getxattr,
  800. .listxattr = xfs_vn_listxattr,
  801. .removexattr = xfs_vn_removexattr,
  802. };