xfs_iops.c 19 KB

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