xfs_iops.c 20 KB

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