xfs_iops.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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_dir.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_quota.h"
  31. #include "xfs_mount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir_sf.h"
  36. #include "xfs_dir2_sf.h"
  37. #include "xfs_attr_sf.h"
  38. #include "xfs_dinode.h"
  39. #include "xfs_inode.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_btree.h"
  42. #include "xfs_ialloc.h"
  43. #include "xfs_rtalloc.h"
  44. #include "xfs_error.h"
  45. #include "xfs_itable.h"
  46. #include "xfs_rw.h"
  47. #include "xfs_acl.h"
  48. #include "xfs_cap.h"
  49. #include "xfs_mac.h"
  50. #include "xfs_attr.h"
  51. #include "xfs_buf_item.h"
  52. #include "xfs_utils.h"
  53. #include <linux/xattr.h>
  54. #include <linux/namei.h>
  55. #include <linux/security.h>
  56. #define IS_NOATIME(inode) ((inode->i_sb->s_flags & MS_NOATIME) || \
  57. (S_ISDIR(inode->i_mode) && inode->i_sb->s_flags & MS_NODIRATIME))
  58. /*
  59. * Get a XFS inode from a given vnode.
  60. */
  61. xfs_inode_t *
  62. xfs_vtoi(
  63. struct vnode *vp)
  64. {
  65. bhv_desc_t *bdp;
  66. bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
  67. VNODE_POSITION_XFS, VNODE_POSITION_XFS);
  68. if (unlikely(bdp == NULL))
  69. return NULL;
  70. return XFS_BHVTOI(bdp);
  71. }
  72. /*
  73. * Bring the atime in the XFS inode uptodate.
  74. * Used before logging the inode to disk or when the Linux inode goes away.
  75. */
  76. void
  77. xfs_synchronize_atime(
  78. xfs_inode_t *ip)
  79. {
  80. vnode_t *vp;
  81. vp = XFS_ITOV_NULL(ip);
  82. if (vp) {
  83. struct inode *inode = &vp->v_inode;
  84. ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
  85. ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
  86. }
  87. }
  88. /*
  89. * Change the requested timestamp in the given inode.
  90. * We don't lock across timestamp updates, and we don't log them but
  91. * we do record the fact that there is dirty information in core.
  92. *
  93. * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
  94. * with XFS_ICHGTIME_ACC to be sure that access time
  95. * update will take. Calling first with XFS_ICHGTIME_ACC
  96. * and then XFS_ICHGTIME_MOD may fail to modify the access
  97. * timestamp if the filesystem is mounted noacctm.
  98. */
  99. void
  100. xfs_ichgtime(
  101. xfs_inode_t *ip,
  102. int flags)
  103. {
  104. struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
  105. timespec_t tv;
  106. nanotime(&tv);
  107. if (flags & XFS_ICHGTIME_MOD) {
  108. inode->i_mtime = tv;
  109. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  110. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  111. }
  112. if (flags & XFS_ICHGTIME_ACC) {
  113. inode->i_atime = tv;
  114. ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
  115. ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
  116. }
  117. if (flags & XFS_ICHGTIME_CHG) {
  118. inode->i_ctime = tv;
  119. ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
  120. ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
  121. }
  122. /*
  123. * We update the i_update_core field _after_ changing
  124. * the timestamps in order to coordinate properly with
  125. * xfs_iflush() so that we don't lose timestamp updates.
  126. * This keeps us from having to hold the inode lock
  127. * while doing this. We use the SYNCHRONIZE macro to
  128. * ensure that the compiler does not reorder the update
  129. * of i_update_core above the timestamp updates above.
  130. */
  131. SYNCHRONIZE();
  132. ip->i_update_core = 1;
  133. if (!(inode->i_state & I_LOCK))
  134. mark_inode_dirty_sync(inode);
  135. }
  136. /*
  137. * Variant on the above which avoids querying the system clock
  138. * in situations where we know the Linux inode timestamps have
  139. * just been updated (and so we can update our inode cheaply).
  140. */
  141. void
  142. xfs_ichgtime_fast(
  143. xfs_inode_t *ip,
  144. struct inode *inode,
  145. int flags)
  146. {
  147. timespec_t *tvp;
  148. /*
  149. * Atime updates for read() & friends are handled lazily now, and
  150. * explicit updates must go through xfs_ichgtime()
  151. */
  152. ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
  153. /*
  154. * We're not supposed to change timestamps in readonly-mounted
  155. * filesystems. Throw it away if anyone asks us.
  156. */
  157. if (unlikely(IS_RDONLY(inode)))
  158. return;
  159. if (flags & XFS_ICHGTIME_MOD) {
  160. tvp = &inode->i_mtime;
  161. ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
  162. ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
  163. }
  164. if (flags & XFS_ICHGTIME_CHG) {
  165. tvp = &inode->i_ctime;
  166. ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
  167. ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
  168. }
  169. /*
  170. * We update the i_update_core field _after_ changing
  171. * the timestamps in order to coordinate properly with
  172. * xfs_iflush() so that we don't lose timestamp updates.
  173. * This keeps us from having to hold the inode lock
  174. * while doing this. We use the SYNCHRONIZE macro to
  175. * ensure that the compiler does not reorder the update
  176. * of i_update_core above the timestamp updates above.
  177. */
  178. SYNCHRONIZE();
  179. ip->i_update_core = 1;
  180. if (!(inode->i_state & I_LOCK))
  181. mark_inode_dirty_sync(inode);
  182. }
  183. /*
  184. * Pull the link count and size up from the xfs inode to the linux inode
  185. */
  186. STATIC void
  187. validate_fields(
  188. struct inode *ip)
  189. {
  190. vnode_t *vp = LINVFS_GET_VP(ip);
  191. vattr_t va;
  192. int error;
  193. va.va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
  194. VOP_GETATTR(vp, &va, ATTR_LAZY, NULL, error);
  195. if (likely(!error)) {
  196. ip->i_nlink = va.va_nlink;
  197. ip->i_blocks = va.va_nblocks;
  198. /* we're under i_mutex so i_size can't change under us */
  199. if (i_size_read(ip) != va.va_size)
  200. i_size_write(ip, va.va_size);
  201. }
  202. }
  203. /*
  204. * Hook in SELinux. This is not quite correct yet, what we really need
  205. * here (as we do for default ACLs) is a mechanism by which creation of
  206. * these attrs can be journalled at inode creation time (along with the
  207. * inode, of course, such that log replay can't cause these to be lost).
  208. */
  209. STATIC int
  210. linvfs_init_security(
  211. struct vnode *vp,
  212. struct inode *dir)
  213. {
  214. struct inode *ip = LINVFS_GET_IP(vp);
  215. size_t length;
  216. void *value;
  217. char *name;
  218. int error;
  219. error = security_inode_init_security(ip, dir, &name, &value, &length);
  220. if (error) {
  221. if (error == -EOPNOTSUPP)
  222. return 0;
  223. return -error;
  224. }
  225. VOP_ATTR_SET(vp, name, value, length, ATTR_SECURE, NULL, error);
  226. if (!error)
  227. VMODIFY(vp);
  228. kfree(name);
  229. kfree(value);
  230. return error;
  231. }
  232. /*
  233. * Determine whether a process has a valid fs_struct (kernel daemons
  234. * like knfsd don't have an fs_struct).
  235. *
  236. * XXX(hch): nfsd is broken, better fix it instead.
  237. */
  238. STATIC inline int
  239. has_fs_struct(struct task_struct *task)
  240. {
  241. return (task->fs != init_task.fs);
  242. }
  243. STATIC int
  244. linvfs_mknod(
  245. struct inode *dir,
  246. struct dentry *dentry,
  247. int mode,
  248. dev_t rdev)
  249. {
  250. struct inode *ip;
  251. vattr_t va;
  252. vnode_t *vp = NULL, *dvp = LINVFS_GET_VP(dir);
  253. xfs_acl_t *default_acl = NULL;
  254. attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
  255. int error;
  256. /*
  257. * Irix uses Missed'em'V split, but doesn't want to see
  258. * the upper 5 bits of (14bit) major.
  259. */
  260. if (!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)
  261. return -EINVAL;
  262. if (test_default_acl && test_default_acl(dvp)) {
  263. if (!_ACL_ALLOC(default_acl))
  264. return -ENOMEM;
  265. if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
  266. _ACL_FREE(default_acl);
  267. default_acl = NULL;
  268. }
  269. }
  270. if (IS_POSIXACL(dir) && !default_acl && has_fs_struct(current))
  271. mode &= ~current->fs->umask;
  272. memset(&va, 0, sizeof(va));
  273. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  274. va.va_mode = mode;
  275. switch (mode & S_IFMT) {
  276. case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
  277. va.va_rdev = sysv_encode_dev(rdev);
  278. va.va_mask |= XFS_AT_RDEV;
  279. /*FALLTHROUGH*/
  280. case S_IFREG:
  281. VOP_CREATE(dvp, dentry, &va, &vp, NULL, error);
  282. break;
  283. case S_IFDIR:
  284. VOP_MKDIR(dvp, dentry, &va, &vp, NULL, error);
  285. break;
  286. default:
  287. error = EINVAL;
  288. break;
  289. }
  290. if (!error)
  291. error = linvfs_init_security(vp, dir);
  292. if (default_acl) {
  293. if (!error) {
  294. error = _ACL_INHERIT(vp, &va, default_acl);
  295. if (!error) {
  296. VMODIFY(vp);
  297. } else {
  298. struct dentry teardown = {};
  299. int err2;
  300. /* Oh, the horror.
  301. * If we can't add the ACL we must back out.
  302. * ENOSPC can hit here, among other things.
  303. */
  304. teardown.d_inode = ip = LINVFS_GET_IP(vp);
  305. teardown.d_name = dentry->d_name;
  306. if (S_ISDIR(mode))
  307. VOP_RMDIR(dvp, &teardown, NULL, err2);
  308. else
  309. VOP_REMOVE(dvp, &teardown, NULL, err2);
  310. VN_RELE(vp);
  311. }
  312. }
  313. _ACL_FREE(default_acl);
  314. }
  315. if (!error) {
  316. ASSERT(vp);
  317. ip = LINVFS_GET_IP(vp);
  318. if (S_ISCHR(mode) || S_ISBLK(mode))
  319. ip->i_rdev = rdev;
  320. else if (S_ISDIR(mode))
  321. validate_fields(ip);
  322. d_instantiate(dentry, ip);
  323. validate_fields(dir);
  324. }
  325. return -error;
  326. }
  327. STATIC int
  328. linvfs_create(
  329. struct inode *dir,
  330. struct dentry *dentry,
  331. int mode,
  332. struct nameidata *nd)
  333. {
  334. return linvfs_mknod(dir, dentry, mode, 0);
  335. }
  336. STATIC int
  337. linvfs_mkdir(
  338. struct inode *dir,
  339. struct dentry *dentry,
  340. int mode)
  341. {
  342. return linvfs_mknod(dir, dentry, mode|S_IFDIR, 0);
  343. }
  344. STATIC struct dentry *
  345. linvfs_lookup(
  346. struct inode *dir,
  347. struct dentry *dentry,
  348. struct nameidata *nd)
  349. {
  350. struct vnode *vp = LINVFS_GET_VP(dir), *cvp;
  351. int error;
  352. if (dentry->d_name.len >= MAXNAMELEN)
  353. return ERR_PTR(-ENAMETOOLONG);
  354. VOP_LOOKUP(vp, dentry, &cvp, 0, NULL, NULL, error);
  355. if (error) {
  356. if (unlikely(error != ENOENT))
  357. return ERR_PTR(-error);
  358. d_add(dentry, NULL);
  359. return NULL;
  360. }
  361. return d_splice_alias(LINVFS_GET_IP(cvp), dentry);
  362. }
  363. STATIC int
  364. linvfs_link(
  365. struct dentry *old_dentry,
  366. struct inode *dir,
  367. struct dentry *dentry)
  368. {
  369. struct inode *ip; /* inode of guy being linked to */
  370. vnode_t *tdvp; /* target directory for new name/link */
  371. vnode_t *vp; /* vp of name being linked */
  372. int error;
  373. ip = old_dentry->d_inode; /* inode being linked to */
  374. if (S_ISDIR(ip->i_mode))
  375. return -EPERM;
  376. tdvp = LINVFS_GET_VP(dir);
  377. vp = LINVFS_GET_VP(ip);
  378. VOP_LINK(tdvp, vp, dentry, NULL, error);
  379. if (!error) {
  380. VMODIFY(tdvp);
  381. VN_HOLD(vp);
  382. validate_fields(ip);
  383. d_instantiate(dentry, ip);
  384. }
  385. return -error;
  386. }
  387. STATIC int
  388. linvfs_unlink(
  389. struct inode *dir,
  390. struct dentry *dentry)
  391. {
  392. struct inode *inode;
  393. vnode_t *dvp; /* directory containing name to remove */
  394. int error;
  395. inode = dentry->d_inode;
  396. dvp = LINVFS_GET_VP(dir);
  397. VOP_REMOVE(dvp, dentry, NULL, error);
  398. if (!error) {
  399. validate_fields(dir); /* For size only */
  400. validate_fields(inode);
  401. }
  402. return -error;
  403. }
  404. STATIC int
  405. linvfs_symlink(
  406. struct inode *dir,
  407. struct dentry *dentry,
  408. const char *symname)
  409. {
  410. struct inode *ip;
  411. vattr_t va;
  412. vnode_t *dvp; /* directory containing name of symlink */
  413. vnode_t *cvp; /* used to lookup symlink to put in dentry */
  414. int error;
  415. dvp = LINVFS_GET_VP(dir);
  416. cvp = NULL;
  417. memset(&va, 0, sizeof(va));
  418. va.va_mode = S_IFLNK |
  419. (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
  420. va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
  421. error = 0;
  422. VOP_SYMLINK(dvp, dentry, &va, (char *)symname, &cvp, NULL, error);
  423. if (!error && cvp) {
  424. ip = LINVFS_GET_IP(cvp);
  425. d_instantiate(dentry, ip);
  426. validate_fields(dir);
  427. validate_fields(ip); /* size needs update */
  428. }
  429. return -error;
  430. }
  431. STATIC int
  432. linvfs_rmdir(
  433. struct inode *dir,
  434. struct dentry *dentry)
  435. {
  436. struct inode *inode = dentry->d_inode;
  437. vnode_t *dvp = LINVFS_GET_VP(dir);
  438. int error;
  439. VOP_RMDIR(dvp, dentry, NULL, error);
  440. if (!error) {
  441. validate_fields(inode);
  442. validate_fields(dir);
  443. }
  444. return -error;
  445. }
  446. STATIC int
  447. linvfs_rename(
  448. struct inode *odir,
  449. struct dentry *odentry,
  450. struct inode *ndir,
  451. struct dentry *ndentry)
  452. {
  453. struct inode *new_inode = ndentry->d_inode;
  454. vnode_t *fvp; /* from directory */
  455. vnode_t *tvp; /* target directory */
  456. int error;
  457. fvp = LINVFS_GET_VP(odir);
  458. tvp = LINVFS_GET_VP(ndir);
  459. VOP_RENAME(fvp, odentry, tvp, ndentry, NULL, error);
  460. if (error)
  461. return -error;
  462. if (new_inode)
  463. validate_fields(new_inode);
  464. validate_fields(odir);
  465. if (ndir != odir)
  466. validate_fields(ndir);
  467. return 0;
  468. }
  469. /*
  470. * careful here - this function can get called recursively, so
  471. * we need to be very careful about how much stack we use.
  472. * uio is kmalloced for this reason...
  473. */
  474. STATIC void *
  475. linvfs_follow_link(
  476. struct dentry *dentry,
  477. struct nameidata *nd)
  478. {
  479. vnode_t *vp;
  480. uio_t *uio;
  481. iovec_t iov;
  482. int error;
  483. char *link;
  484. ASSERT(dentry);
  485. ASSERT(nd);
  486. link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  487. if (!link) {
  488. nd_set_link(nd, ERR_PTR(-ENOMEM));
  489. return NULL;
  490. }
  491. uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
  492. if (!uio) {
  493. kfree(link);
  494. nd_set_link(nd, ERR_PTR(-ENOMEM));
  495. return NULL;
  496. }
  497. vp = LINVFS_GET_VP(dentry->d_inode);
  498. iov.iov_base = link;
  499. iov.iov_len = MAXPATHLEN;
  500. uio->uio_iov = &iov;
  501. uio->uio_offset = 0;
  502. uio->uio_segflg = UIO_SYSSPACE;
  503. uio->uio_resid = MAXPATHLEN;
  504. uio->uio_iovcnt = 1;
  505. VOP_READLINK(vp, uio, 0, NULL, error);
  506. if (error) {
  507. kfree(link);
  508. link = ERR_PTR(-error);
  509. } else {
  510. link[MAXPATHLEN - uio->uio_resid] = '\0';
  511. }
  512. kfree(uio);
  513. nd_set_link(nd, link);
  514. return NULL;
  515. }
  516. STATIC void
  517. linvfs_put_link(
  518. struct dentry *dentry,
  519. struct nameidata *nd,
  520. void *p)
  521. {
  522. char *s = nd_get_link(nd);
  523. if (!IS_ERR(s))
  524. kfree(s);
  525. }
  526. #ifdef CONFIG_XFS_POSIX_ACL
  527. STATIC int
  528. linvfs_permission(
  529. struct inode *inode,
  530. int mode,
  531. struct nameidata *nd)
  532. {
  533. vnode_t *vp = LINVFS_GET_VP(inode);
  534. int error;
  535. mode <<= 6; /* convert from linux to vnode access bits */
  536. VOP_ACCESS(vp, mode, NULL, error);
  537. return -error;
  538. }
  539. #else
  540. #define linvfs_permission NULL
  541. #endif
  542. STATIC int
  543. linvfs_getattr(
  544. struct vfsmount *mnt,
  545. struct dentry *dentry,
  546. struct kstat *stat)
  547. {
  548. struct inode *inode = dentry->d_inode;
  549. vnode_t *vp = LINVFS_GET_VP(inode);
  550. int error = 0;
  551. if (unlikely(vp->v_flag & VMODIFIED))
  552. error = vn_revalidate(vp);
  553. if (!error)
  554. generic_fillattr(inode, stat);
  555. return 0;
  556. }
  557. STATIC int
  558. linvfs_setattr(
  559. struct dentry *dentry,
  560. struct iattr *attr)
  561. {
  562. struct inode *inode = dentry->d_inode;
  563. unsigned int ia_valid = attr->ia_valid;
  564. vnode_t *vp = LINVFS_GET_VP(inode);
  565. vattr_t vattr;
  566. int flags = 0;
  567. int error;
  568. memset(&vattr, 0, sizeof(vattr_t));
  569. if (ia_valid & ATTR_UID) {
  570. vattr.va_mask |= XFS_AT_UID;
  571. vattr.va_uid = attr->ia_uid;
  572. }
  573. if (ia_valid & ATTR_GID) {
  574. vattr.va_mask |= XFS_AT_GID;
  575. vattr.va_gid = attr->ia_gid;
  576. }
  577. if (ia_valid & ATTR_SIZE) {
  578. vattr.va_mask |= XFS_AT_SIZE;
  579. vattr.va_size = attr->ia_size;
  580. }
  581. if (ia_valid & ATTR_ATIME) {
  582. vattr.va_mask |= XFS_AT_ATIME;
  583. vattr.va_atime = attr->ia_atime;
  584. }
  585. if (ia_valid & ATTR_MTIME) {
  586. vattr.va_mask |= XFS_AT_MTIME;
  587. vattr.va_mtime = attr->ia_mtime;
  588. }
  589. if (ia_valid & ATTR_CTIME) {
  590. vattr.va_mask |= XFS_AT_CTIME;
  591. vattr.va_ctime = attr->ia_ctime;
  592. }
  593. if (ia_valid & ATTR_MODE) {
  594. vattr.va_mask |= XFS_AT_MODE;
  595. vattr.va_mode = attr->ia_mode;
  596. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  597. inode->i_mode &= ~S_ISGID;
  598. }
  599. if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
  600. flags |= ATTR_UTIME;
  601. #ifdef ATTR_NO_BLOCK
  602. if ((ia_valid & ATTR_NO_BLOCK))
  603. flags |= ATTR_NONBLOCK;
  604. #endif
  605. VOP_SETATTR(vp, &vattr, flags, NULL, error);
  606. if (error)
  607. return -error;
  608. vn_revalidate(vp);
  609. return error;
  610. }
  611. STATIC void
  612. linvfs_truncate(
  613. struct inode *inode)
  614. {
  615. block_truncate_page(inode->i_mapping, inode->i_size, linvfs_get_block);
  616. }
  617. STATIC int
  618. linvfs_setxattr(
  619. struct dentry *dentry,
  620. const char *name,
  621. const void *data,
  622. size_t size,
  623. int flags)
  624. {
  625. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  626. char *attr = (char *)name;
  627. attrnames_t *namesp;
  628. int xflags = 0;
  629. int error;
  630. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  631. if (!namesp)
  632. return -EOPNOTSUPP;
  633. attr += namesp->attr_namelen;
  634. error = namesp->attr_capable(vp, NULL);
  635. if (error)
  636. return error;
  637. /* Convert Linux syscall to XFS internal ATTR flags */
  638. if (flags & XATTR_CREATE)
  639. xflags |= ATTR_CREATE;
  640. if (flags & XATTR_REPLACE)
  641. xflags |= ATTR_REPLACE;
  642. xflags |= namesp->attr_flag;
  643. return namesp->attr_set(vp, attr, (void *)data, size, xflags);
  644. }
  645. STATIC ssize_t
  646. linvfs_getxattr(
  647. struct dentry *dentry,
  648. const char *name,
  649. void *data,
  650. size_t size)
  651. {
  652. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  653. char *attr = (char *)name;
  654. attrnames_t *namesp;
  655. int xflags = 0;
  656. ssize_t error;
  657. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  658. if (!namesp)
  659. return -EOPNOTSUPP;
  660. attr += namesp->attr_namelen;
  661. error = namesp->attr_capable(vp, NULL);
  662. if (error)
  663. return error;
  664. /* Convert Linux syscall to XFS internal ATTR flags */
  665. if (!size) {
  666. xflags |= ATTR_KERNOVAL;
  667. data = NULL;
  668. }
  669. xflags |= namesp->attr_flag;
  670. return namesp->attr_get(vp, attr, (void *)data, size, xflags);
  671. }
  672. STATIC ssize_t
  673. linvfs_listxattr(
  674. struct dentry *dentry,
  675. char *data,
  676. size_t size)
  677. {
  678. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  679. int error, xflags = ATTR_KERNAMELS;
  680. ssize_t result;
  681. if (!size)
  682. xflags |= ATTR_KERNOVAL;
  683. xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
  684. error = attr_generic_list(vp, data, size, xflags, &result);
  685. if (error < 0)
  686. return error;
  687. return result;
  688. }
  689. STATIC int
  690. linvfs_removexattr(
  691. struct dentry *dentry,
  692. const char *name)
  693. {
  694. vnode_t *vp = LINVFS_GET_VP(dentry->d_inode);
  695. char *attr = (char *)name;
  696. attrnames_t *namesp;
  697. int xflags = 0;
  698. int error;
  699. namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
  700. if (!namesp)
  701. return -EOPNOTSUPP;
  702. attr += namesp->attr_namelen;
  703. error = namesp->attr_capable(vp, NULL);
  704. if (error)
  705. return error;
  706. xflags |= namesp->attr_flag;
  707. return namesp->attr_remove(vp, attr, xflags);
  708. }
  709. struct inode_operations linvfs_file_inode_operations = {
  710. .permission = linvfs_permission,
  711. .truncate = linvfs_truncate,
  712. .getattr = linvfs_getattr,
  713. .setattr = linvfs_setattr,
  714. .setxattr = linvfs_setxattr,
  715. .getxattr = linvfs_getxattr,
  716. .listxattr = linvfs_listxattr,
  717. .removexattr = linvfs_removexattr,
  718. };
  719. struct inode_operations linvfs_dir_inode_operations = {
  720. .create = linvfs_create,
  721. .lookup = linvfs_lookup,
  722. .link = linvfs_link,
  723. .unlink = linvfs_unlink,
  724. .symlink = linvfs_symlink,
  725. .mkdir = linvfs_mkdir,
  726. .rmdir = linvfs_rmdir,
  727. .mknod = linvfs_mknod,
  728. .rename = linvfs_rename,
  729. .permission = linvfs_permission,
  730. .getattr = linvfs_getattr,
  731. .setattr = linvfs_setattr,
  732. .setxattr = linvfs_setxattr,
  733. .getxattr = linvfs_getxattr,
  734. .listxattr = linvfs_listxattr,
  735. .removexattr = linvfs_removexattr,
  736. };
  737. struct inode_operations linvfs_symlink_inode_operations = {
  738. .readlink = generic_readlink,
  739. .follow_link = linvfs_follow_link,
  740. .put_link = linvfs_put_link,
  741. .permission = linvfs_permission,
  742. .getattr = linvfs_getattr,
  743. .setattr = linvfs_setattr,
  744. .setxattr = linvfs_setxattr,
  745. .getxattr = linvfs_getxattr,
  746. .listxattr = linvfs_listxattr,
  747. .removexattr = linvfs_removexattr,
  748. };