xfs_iops.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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_acl.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_alloc.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_attr.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_utils.h"
  38. #include "xfs_vnodeops.h"
  39. #include "xfs_inode_item.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_icache.h"
  42. #include <linux/capability.h>
  43. #include <linux/xattr.h>
  44. #include <linux/namei.h>
  45. #include <linux/posix_acl.h>
  46. #include <linux/security.h>
  47. #include <linux/fiemap.h>
  48. #include <linux/slab.h>
  49. static int
  50. xfs_initxattrs(
  51. struct inode *inode,
  52. const struct xattr *xattr_array,
  53. void *fs_info)
  54. {
  55. const struct xattr *xattr;
  56. struct xfs_inode *ip = XFS_I(inode);
  57. int error = 0;
  58. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  59. error = xfs_attr_set(ip, xattr->name, xattr->value,
  60. xattr->value_len, ATTR_SECURE);
  61. if (error < 0)
  62. break;
  63. }
  64. return error;
  65. }
  66. /*
  67. * Hook in SELinux. This is not quite correct yet, what we really need
  68. * here (as we do for default ACLs) is a mechanism by which creation of
  69. * these attrs can be journalled at inode creation time (along with the
  70. * inode, of course, such that log replay can't cause these to be lost).
  71. */
  72. STATIC int
  73. xfs_init_security(
  74. struct inode *inode,
  75. struct inode *dir,
  76. const struct qstr *qstr)
  77. {
  78. return security_inode_init_security(inode, dir, qstr,
  79. &xfs_initxattrs, NULL);
  80. }
  81. static void
  82. xfs_dentry_to_name(
  83. struct xfs_name *namep,
  84. struct dentry *dentry)
  85. {
  86. namep->name = dentry->d_name.name;
  87. namep->len = dentry->d_name.len;
  88. }
  89. STATIC void
  90. xfs_cleanup_inode(
  91. struct inode *dir,
  92. struct inode *inode,
  93. struct dentry *dentry)
  94. {
  95. struct xfs_name teardown;
  96. /* Oh, the horror.
  97. * If we can't add the ACL or we fail in
  98. * xfs_init_security we must back out.
  99. * ENOSPC can hit here, among other things.
  100. */
  101. xfs_dentry_to_name(&teardown, dentry);
  102. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  103. iput(inode);
  104. }
  105. STATIC int
  106. xfs_vn_mknod(
  107. struct inode *dir,
  108. struct dentry *dentry,
  109. umode_t mode,
  110. dev_t rdev)
  111. {
  112. struct inode *inode;
  113. struct xfs_inode *ip = NULL;
  114. struct posix_acl *default_acl = NULL;
  115. struct xfs_name name;
  116. int error;
  117. /*
  118. * Irix uses Missed'em'V split, but doesn't want to see
  119. * the upper 5 bits of (14bit) major.
  120. */
  121. if (S_ISCHR(mode) || S_ISBLK(mode)) {
  122. if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
  123. return -EINVAL;
  124. rdev = sysv_encode_dev(rdev);
  125. } else {
  126. rdev = 0;
  127. }
  128. if (IS_POSIXACL(dir)) {
  129. default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
  130. if (IS_ERR(default_acl))
  131. return PTR_ERR(default_acl);
  132. if (!default_acl)
  133. mode &= ~current_umask();
  134. }
  135. xfs_dentry_to_name(&name, dentry);
  136. error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
  137. if (unlikely(error))
  138. goto out_free_acl;
  139. inode = VFS_I(ip);
  140. error = xfs_init_security(inode, dir, &dentry->d_name);
  141. if (unlikely(error))
  142. goto out_cleanup_inode;
  143. if (default_acl) {
  144. error = -xfs_inherit_acl(inode, default_acl);
  145. default_acl = NULL;
  146. if (unlikely(error))
  147. goto out_cleanup_inode;
  148. }
  149. d_instantiate(dentry, inode);
  150. return -error;
  151. out_cleanup_inode:
  152. xfs_cleanup_inode(dir, inode, dentry);
  153. out_free_acl:
  154. posix_acl_release(default_acl);
  155. return -error;
  156. }
  157. STATIC int
  158. xfs_vn_create(
  159. struct inode *dir,
  160. struct dentry *dentry,
  161. umode_t mode,
  162. bool flags)
  163. {
  164. return xfs_vn_mknod(dir, dentry, mode, 0);
  165. }
  166. STATIC int
  167. xfs_vn_mkdir(
  168. struct inode *dir,
  169. struct dentry *dentry,
  170. umode_t mode)
  171. {
  172. return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
  173. }
  174. STATIC struct dentry *
  175. xfs_vn_lookup(
  176. struct inode *dir,
  177. struct dentry *dentry,
  178. unsigned int flags)
  179. {
  180. struct xfs_inode *cip;
  181. struct xfs_name name;
  182. int error;
  183. if (dentry->d_name.len >= MAXNAMELEN)
  184. return ERR_PTR(-ENAMETOOLONG);
  185. xfs_dentry_to_name(&name, dentry);
  186. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  187. if (unlikely(error)) {
  188. if (unlikely(error != ENOENT))
  189. return ERR_PTR(-error);
  190. d_add(dentry, NULL);
  191. return NULL;
  192. }
  193. return d_splice_alias(VFS_I(cip), dentry);
  194. }
  195. STATIC struct dentry *
  196. xfs_vn_ci_lookup(
  197. struct inode *dir,
  198. struct dentry *dentry,
  199. unsigned int flags)
  200. {
  201. struct xfs_inode *ip;
  202. struct xfs_name xname;
  203. struct xfs_name ci_name;
  204. struct qstr dname;
  205. int error;
  206. if (dentry->d_name.len >= MAXNAMELEN)
  207. return ERR_PTR(-ENAMETOOLONG);
  208. xfs_dentry_to_name(&xname, dentry);
  209. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  210. if (unlikely(error)) {
  211. if (unlikely(error != ENOENT))
  212. return ERR_PTR(-error);
  213. /*
  214. * call d_add(dentry, NULL) here when d_drop_negative_children
  215. * is called in xfs_vn_mknod (ie. allow negative dentries
  216. * with CI filesystems).
  217. */
  218. return NULL;
  219. }
  220. /* if exact match, just splice and exit */
  221. if (!ci_name.name)
  222. return d_splice_alias(VFS_I(ip), dentry);
  223. /* else case-insensitive match... */
  224. dname.name = ci_name.name;
  225. dname.len = ci_name.len;
  226. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  227. kmem_free(ci_name.name);
  228. return dentry;
  229. }
  230. STATIC int
  231. xfs_vn_link(
  232. struct dentry *old_dentry,
  233. struct inode *dir,
  234. struct dentry *dentry)
  235. {
  236. struct inode *inode = old_dentry->d_inode;
  237. struct xfs_name name;
  238. int error;
  239. xfs_dentry_to_name(&name, dentry);
  240. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  241. if (unlikely(error))
  242. return -error;
  243. ihold(inode);
  244. d_instantiate(dentry, inode);
  245. return 0;
  246. }
  247. STATIC int
  248. xfs_vn_unlink(
  249. struct inode *dir,
  250. struct dentry *dentry)
  251. {
  252. struct xfs_name name;
  253. int error;
  254. xfs_dentry_to_name(&name, dentry);
  255. error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
  256. if (error)
  257. return error;
  258. /*
  259. * With unlink, the VFS makes the dentry "negative": no inode,
  260. * but still hashed. This is incompatible with case-insensitive
  261. * mode, so invalidate (unhash) the dentry in CI-mode.
  262. */
  263. if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
  264. d_invalidate(dentry);
  265. return 0;
  266. }
  267. STATIC int
  268. xfs_vn_symlink(
  269. struct inode *dir,
  270. struct dentry *dentry,
  271. const char *symname)
  272. {
  273. struct inode *inode;
  274. struct xfs_inode *cip = NULL;
  275. struct xfs_name name;
  276. int error;
  277. umode_t mode;
  278. mode = S_IFLNK |
  279. (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
  280. xfs_dentry_to_name(&name, dentry);
  281. error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
  282. if (unlikely(error))
  283. goto out;
  284. inode = VFS_I(cip);
  285. error = xfs_init_security(inode, dir, &dentry->d_name);
  286. if (unlikely(error))
  287. goto out_cleanup_inode;
  288. d_instantiate(dentry, inode);
  289. return 0;
  290. out_cleanup_inode:
  291. xfs_cleanup_inode(dir, inode, dentry);
  292. out:
  293. return -error;
  294. }
  295. STATIC int
  296. xfs_vn_rename(
  297. struct inode *odir,
  298. struct dentry *odentry,
  299. struct inode *ndir,
  300. struct dentry *ndentry)
  301. {
  302. struct inode *new_inode = ndentry->d_inode;
  303. struct xfs_name oname;
  304. struct xfs_name nname;
  305. xfs_dentry_to_name(&oname, odentry);
  306. xfs_dentry_to_name(&nname, ndentry);
  307. return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
  308. XFS_I(ndir), &nname, new_inode ?
  309. XFS_I(new_inode) : NULL);
  310. }
  311. /*
  312. * careful here - this function can get called recursively, so
  313. * we need to be very careful about how much stack we use.
  314. * uio is kmalloced for this reason...
  315. */
  316. STATIC void *
  317. xfs_vn_follow_link(
  318. struct dentry *dentry,
  319. struct nameidata *nd)
  320. {
  321. char *link;
  322. int error = -ENOMEM;
  323. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  324. if (!link)
  325. goto out_err;
  326. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  327. if (unlikely(error))
  328. goto out_kfree;
  329. nd_set_link(nd, link);
  330. return NULL;
  331. out_kfree:
  332. kfree(link);
  333. out_err:
  334. nd_set_link(nd, ERR_PTR(error));
  335. return NULL;
  336. }
  337. STATIC void
  338. xfs_vn_put_link(
  339. struct dentry *dentry,
  340. struct nameidata *nd,
  341. void *p)
  342. {
  343. char *s = nd_get_link(nd);
  344. if (!IS_ERR(s))
  345. kfree(s);
  346. }
  347. STATIC int
  348. xfs_vn_getattr(
  349. struct vfsmount *mnt,
  350. struct dentry *dentry,
  351. struct kstat *stat)
  352. {
  353. struct inode *inode = dentry->d_inode;
  354. struct xfs_inode *ip = XFS_I(inode);
  355. struct xfs_mount *mp = ip->i_mount;
  356. trace_xfs_getattr(ip);
  357. if (XFS_FORCED_SHUTDOWN(mp))
  358. return -XFS_ERROR(EIO);
  359. stat->size = XFS_ISIZE(ip);
  360. stat->dev = inode->i_sb->s_dev;
  361. stat->mode = ip->i_d.di_mode;
  362. stat->nlink = ip->i_d.di_nlink;
  363. stat->uid = ip->i_d.di_uid;
  364. stat->gid = ip->i_d.di_gid;
  365. stat->ino = ip->i_ino;
  366. stat->atime = inode->i_atime;
  367. stat->mtime = inode->i_mtime;
  368. stat->ctime = inode->i_ctime;
  369. stat->blocks =
  370. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  371. switch (inode->i_mode & S_IFMT) {
  372. case S_IFBLK:
  373. case S_IFCHR:
  374. stat->blksize = BLKDEV_IOSIZE;
  375. stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  376. sysv_minor(ip->i_df.if_u2.if_rdev));
  377. break;
  378. default:
  379. if (XFS_IS_REALTIME_INODE(ip)) {
  380. /*
  381. * If the file blocks are being allocated from a
  382. * realtime volume, then return the inode's realtime
  383. * extent size or the realtime volume's extent size.
  384. */
  385. stat->blksize =
  386. xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
  387. } else
  388. stat->blksize = xfs_preferred_iosize(mp);
  389. stat->rdev = 0;
  390. break;
  391. }
  392. return 0;
  393. }
  394. static void
  395. xfs_setattr_mode(
  396. struct xfs_trans *tp,
  397. struct xfs_inode *ip,
  398. struct iattr *iattr)
  399. {
  400. struct inode *inode = VFS_I(ip);
  401. umode_t mode = iattr->ia_mode;
  402. ASSERT(tp);
  403. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  404. ip->i_d.di_mode &= S_IFMT;
  405. ip->i_d.di_mode |= mode & ~S_IFMT;
  406. inode->i_mode &= S_IFMT;
  407. inode->i_mode |= mode & ~S_IFMT;
  408. }
  409. int
  410. xfs_setattr_nonsize(
  411. struct xfs_inode *ip,
  412. struct iattr *iattr,
  413. int flags)
  414. {
  415. xfs_mount_t *mp = ip->i_mount;
  416. struct inode *inode = VFS_I(ip);
  417. int mask = iattr->ia_valid;
  418. xfs_trans_t *tp;
  419. int error;
  420. uid_t uid = 0, iuid = 0;
  421. gid_t gid = 0, igid = 0;
  422. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  423. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  424. trace_xfs_setattr(ip);
  425. /* If acls are being inherited, we already have this checked */
  426. if (!(flags & XFS_ATTR_NOACL)) {
  427. if (mp->m_flags & XFS_MOUNT_RDONLY)
  428. return XFS_ERROR(EROFS);
  429. if (XFS_FORCED_SHUTDOWN(mp))
  430. return XFS_ERROR(EIO);
  431. error = -inode_change_ok(inode, iattr);
  432. if (error)
  433. return XFS_ERROR(error);
  434. }
  435. ASSERT((mask & ATTR_SIZE) == 0);
  436. /*
  437. * If disk quotas is on, we make sure that the dquots do exist on disk,
  438. * before we start any other transactions. Trying to do this later
  439. * is messy. We don't care to take a readlock to look at the ids
  440. * in inode here, because we can't hold it across the trans_reserve.
  441. * If the IDs do change before we take the ilock, we're covered
  442. * because the i_*dquot fields will get updated anyway.
  443. */
  444. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  445. uint qflags = 0;
  446. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  447. uid = iattr->ia_uid;
  448. qflags |= XFS_QMOPT_UQUOTA;
  449. } else {
  450. uid = ip->i_d.di_uid;
  451. }
  452. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  453. gid = iattr->ia_gid;
  454. qflags |= XFS_QMOPT_GQUOTA;
  455. } else {
  456. gid = ip->i_d.di_gid;
  457. }
  458. /*
  459. * We take a reference when we initialize udqp and gdqp,
  460. * so it is important that we never blindly double trip on
  461. * the same variable. See xfs_create() for an example.
  462. */
  463. ASSERT(udqp == NULL);
  464. ASSERT(gdqp == NULL);
  465. error = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
  466. qflags, &udqp, &gdqp, NULL);
  467. if (error)
  468. return error;
  469. }
  470. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  471. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  472. if (error)
  473. goto out_dqrele;
  474. xfs_ilock(ip, XFS_ILOCK_EXCL);
  475. /*
  476. * Change file ownership. Must be the owner or privileged.
  477. */
  478. if (mask & (ATTR_UID|ATTR_GID)) {
  479. /*
  480. * These IDs could have changed since we last looked at them.
  481. * But, we're assured that if the ownership did change
  482. * while we didn't have the inode locked, inode's dquot(s)
  483. * would have changed also.
  484. */
  485. iuid = ip->i_d.di_uid;
  486. igid = ip->i_d.di_gid;
  487. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  488. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  489. /*
  490. * Do a quota reservation only if uid/gid is actually
  491. * going to change.
  492. */
  493. if (XFS_IS_QUOTA_RUNNING(mp) &&
  494. ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  495. (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
  496. ASSERT(tp);
  497. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  498. NULL, capable(CAP_FOWNER) ?
  499. XFS_QMOPT_FORCE_RES : 0);
  500. if (error) /* out of quota */
  501. goto out_trans_cancel;
  502. }
  503. }
  504. xfs_trans_ijoin(tp, ip, 0);
  505. /*
  506. * Change file ownership. Must be the owner or privileged.
  507. */
  508. if (mask & (ATTR_UID|ATTR_GID)) {
  509. /*
  510. * CAP_FSETID overrides the following restrictions:
  511. *
  512. * The set-user-ID and set-group-ID bits of a file will be
  513. * cleared upon successful return from chown()
  514. */
  515. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  516. !capable(CAP_FSETID))
  517. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  518. /*
  519. * Change the ownerships and register quota modifications
  520. * in the transaction.
  521. */
  522. if (iuid != uid) {
  523. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  524. ASSERT(mask & ATTR_UID);
  525. ASSERT(udqp);
  526. olddquot1 = xfs_qm_vop_chown(tp, ip,
  527. &ip->i_udquot, udqp);
  528. }
  529. ip->i_d.di_uid = uid;
  530. inode->i_uid = uid;
  531. }
  532. if (igid != gid) {
  533. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  534. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  535. ASSERT(mask & ATTR_GID);
  536. ASSERT(gdqp);
  537. olddquot2 = xfs_qm_vop_chown(tp, ip,
  538. &ip->i_gdquot, gdqp);
  539. }
  540. ip->i_d.di_gid = gid;
  541. inode->i_gid = gid;
  542. }
  543. }
  544. /*
  545. * Change file access modes.
  546. */
  547. if (mask & ATTR_MODE)
  548. xfs_setattr_mode(tp, ip, iattr);
  549. /*
  550. * Change file access or modified times.
  551. */
  552. if (mask & ATTR_ATIME) {
  553. inode->i_atime = iattr->ia_atime;
  554. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  555. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  556. }
  557. if (mask & ATTR_CTIME) {
  558. inode->i_ctime = iattr->ia_ctime;
  559. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  560. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  561. }
  562. if (mask & ATTR_MTIME) {
  563. inode->i_mtime = iattr->ia_mtime;
  564. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  565. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  566. }
  567. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  568. XFS_STATS_INC(xs_ig_attrchg);
  569. if (mp->m_flags & XFS_MOUNT_WSYNC)
  570. xfs_trans_set_sync(tp);
  571. error = xfs_trans_commit(tp, 0);
  572. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  573. /*
  574. * Release any dquot(s) the inode had kept before chown.
  575. */
  576. xfs_qm_dqrele(olddquot1);
  577. xfs_qm_dqrele(olddquot2);
  578. xfs_qm_dqrele(udqp);
  579. xfs_qm_dqrele(gdqp);
  580. if (error)
  581. return XFS_ERROR(error);
  582. /*
  583. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  584. * update. We could avoid this with linked transactions
  585. * and passing down the transaction pointer all the way
  586. * to attr_set. No previous user of the generic
  587. * Posix ACL code seems to care about this issue either.
  588. */
  589. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  590. error = -xfs_acl_chmod(inode);
  591. if (error)
  592. return XFS_ERROR(error);
  593. }
  594. return 0;
  595. out_trans_cancel:
  596. xfs_trans_cancel(tp, 0);
  597. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  598. out_dqrele:
  599. xfs_qm_dqrele(udqp);
  600. xfs_qm_dqrele(gdqp);
  601. return error;
  602. }
  603. /*
  604. * Truncate file. Must have write permission and not be a directory.
  605. */
  606. int
  607. xfs_setattr_size(
  608. struct xfs_inode *ip,
  609. struct iattr *iattr,
  610. int flags)
  611. {
  612. struct xfs_mount *mp = ip->i_mount;
  613. struct inode *inode = VFS_I(ip);
  614. int mask = iattr->ia_valid;
  615. xfs_off_t oldsize, newsize;
  616. struct xfs_trans *tp;
  617. int error;
  618. uint lock_flags = 0;
  619. uint commit_flags = 0;
  620. trace_xfs_setattr(ip);
  621. if (mp->m_flags & XFS_MOUNT_RDONLY)
  622. return XFS_ERROR(EROFS);
  623. if (XFS_FORCED_SHUTDOWN(mp))
  624. return XFS_ERROR(EIO);
  625. error = -inode_change_ok(inode, iattr);
  626. if (error)
  627. return XFS_ERROR(error);
  628. ASSERT(S_ISREG(ip->i_d.di_mode));
  629. ASSERT((mask & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  630. ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  631. if (!(flags & XFS_ATTR_NOLOCK)) {
  632. lock_flags |= XFS_IOLOCK_EXCL;
  633. xfs_ilock(ip, lock_flags);
  634. }
  635. oldsize = inode->i_size;
  636. newsize = iattr->ia_size;
  637. /*
  638. * Short circuit the truncate case for zero length files.
  639. */
  640. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  641. if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
  642. goto out_unlock;
  643. /*
  644. * Use the regular setattr path to update the timestamps.
  645. */
  646. xfs_iunlock(ip, lock_flags);
  647. iattr->ia_valid &= ~ATTR_SIZE;
  648. return xfs_setattr_nonsize(ip, iattr, 0);
  649. }
  650. /*
  651. * Make sure that the dquots are attached to the inode.
  652. */
  653. error = xfs_qm_dqattach(ip, 0);
  654. if (error)
  655. goto out_unlock;
  656. /*
  657. * Now we can make the changes. Before we join the inode to the
  658. * transaction, take care of the part of the truncation that must be
  659. * done without the inode lock. This needs to be done before joining
  660. * the inode to the transaction, because the inode cannot be unlocked
  661. * once it is a part of the transaction.
  662. */
  663. if (newsize > oldsize) {
  664. /*
  665. * Do the first part of growing a file: zero any data in the
  666. * last block that is beyond the old EOF. We need to do this
  667. * before the inode is joined to the transaction to modify
  668. * i_size.
  669. */
  670. error = xfs_zero_eof(ip, newsize, oldsize);
  671. if (error)
  672. goto out_unlock;
  673. }
  674. /*
  675. * We are going to log the inode size change in this transaction so
  676. * any previous writes that are beyond the on disk EOF and the new
  677. * EOF that have not been written out need to be written here. If we
  678. * do not write the data out, we expose ourselves to the null files
  679. * problem.
  680. *
  681. * Only flush from the on disk size to the smaller of the in memory
  682. * file size or the new size as that's the range we really care about
  683. * here and prevents waiting for other data not within the range we
  684. * care about here.
  685. */
  686. if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
  687. error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  688. ip->i_d.di_size, newsize);
  689. if (error)
  690. goto out_unlock;
  691. }
  692. /*
  693. * Wait for all direct I/O to complete.
  694. */
  695. inode_dio_wait(inode);
  696. error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
  697. if (error)
  698. goto out_unlock;
  699. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  700. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  701. XFS_TRANS_PERM_LOG_RES,
  702. XFS_ITRUNCATE_LOG_COUNT);
  703. if (error)
  704. goto out_trans_cancel;
  705. truncate_setsize(inode, newsize);
  706. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  707. lock_flags |= XFS_ILOCK_EXCL;
  708. xfs_ilock(ip, XFS_ILOCK_EXCL);
  709. xfs_trans_ijoin(tp, ip, 0);
  710. /*
  711. * Only change the c/mtime if we are changing the size or we are
  712. * explicitly asked to change it. This handles the semantic difference
  713. * between truncate() and ftruncate() as implemented in the VFS.
  714. *
  715. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  716. * special case where we need to update the times despite not having
  717. * these flags set. For all other operations the VFS set these flags
  718. * explicitly if it wants a timestamp update.
  719. */
  720. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
  721. iattr->ia_ctime = iattr->ia_mtime =
  722. current_fs_time(inode->i_sb);
  723. mask |= ATTR_CTIME | ATTR_MTIME;
  724. }
  725. /*
  726. * The first thing we do is set the size to new_size permanently on
  727. * disk. This way we don't have to worry about anyone ever being able
  728. * to look at the data being freed even in the face of a crash.
  729. * What we're getting around here is the case where we free a block, it
  730. * is allocated to another file, it is written to, and then we crash.
  731. * If the new data gets written to the file but the log buffers
  732. * containing the free and reallocation don't, then we'd end up with
  733. * garbage in the blocks being freed. As long as we make the new size
  734. * permanent before actually freeing any blocks it doesn't matter if
  735. * they get written to.
  736. */
  737. ip->i_d.di_size = newsize;
  738. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  739. if (newsize <= oldsize) {
  740. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  741. if (error)
  742. goto out_trans_abort;
  743. /*
  744. * Truncated "down", so we're removing references to old data
  745. * here - if we delay flushing for a long time, we expose
  746. * ourselves unduly to the notorious NULL files problem. So,
  747. * we mark this inode and flush it when the file is closed,
  748. * and do not wait the usual (long) time for writeout.
  749. */
  750. xfs_iflags_set(ip, XFS_ITRUNCATED);
  751. /* A truncate down always removes post-EOF blocks. */
  752. xfs_inode_clear_eofblocks_tag(ip);
  753. }
  754. /*
  755. * Change file access modes.
  756. */
  757. if (mask & ATTR_MODE)
  758. xfs_setattr_mode(tp, ip, iattr);
  759. if (mask & ATTR_CTIME) {
  760. inode->i_ctime = iattr->ia_ctime;
  761. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  762. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  763. }
  764. if (mask & ATTR_MTIME) {
  765. inode->i_mtime = iattr->ia_mtime;
  766. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  767. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  768. }
  769. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  770. XFS_STATS_INC(xs_ig_attrchg);
  771. if (mp->m_flags & XFS_MOUNT_WSYNC)
  772. xfs_trans_set_sync(tp);
  773. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  774. out_unlock:
  775. if (lock_flags)
  776. xfs_iunlock(ip, lock_flags);
  777. return error;
  778. out_trans_abort:
  779. commit_flags |= XFS_TRANS_ABORT;
  780. out_trans_cancel:
  781. xfs_trans_cancel(tp, commit_flags);
  782. goto out_unlock;
  783. }
  784. STATIC int
  785. xfs_vn_setattr(
  786. struct dentry *dentry,
  787. struct iattr *iattr)
  788. {
  789. if (iattr->ia_valid & ATTR_SIZE)
  790. return -xfs_setattr_size(XFS_I(dentry->d_inode), iattr, 0);
  791. return -xfs_setattr_nonsize(XFS_I(dentry->d_inode), iattr, 0);
  792. }
  793. STATIC int
  794. xfs_vn_update_time(
  795. struct inode *inode,
  796. struct timespec *now,
  797. int flags)
  798. {
  799. struct xfs_inode *ip = XFS_I(inode);
  800. struct xfs_mount *mp = ip->i_mount;
  801. struct xfs_trans *tp;
  802. int error;
  803. trace_xfs_update_time(ip);
  804. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  805. error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  806. if (error) {
  807. xfs_trans_cancel(tp, 0);
  808. return -error;
  809. }
  810. xfs_ilock(ip, XFS_ILOCK_EXCL);
  811. if (flags & S_CTIME) {
  812. inode->i_ctime = *now;
  813. ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
  814. ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
  815. }
  816. if (flags & S_MTIME) {
  817. inode->i_mtime = *now;
  818. ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
  819. ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
  820. }
  821. if (flags & S_ATIME) {
  822. inode->i_atime = *now;
  823. ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
  824. ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
  825. }
  826. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  827. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  828. return -xfs_trans_commit(tp, 0);
  829. }
  830. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  831. /*
  832. * Call fiemap helper to fill in user data.
  833. * Returns positive errors to xfs_getbmap.
  834. */
  835. STATIC int
  836. xfs_fiemap_format(
  837. void **arg,
  838. struct getbmapx *bmv,
  839. int *full)
  840. {
  841. int error;
  842. struct fiemap_extent_info *fieinfo = *arg;
  843. u32 fiemap_flags = 0;
  844. u64 logical, physical, length;
  845. /* Do nothing for a hole */
  846. if (bmv->bmv_block == -1LL)
  847. return 0;
  848. logical = BBTOB(bmv->bmv_offset);
  849. physical = BBTOB(bmv->bmv_block);
  850. length = BBTOB(bmv->bmv_length);
  851. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  852. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  853. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  854. fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
  855. FIEMAP_EXTENT_UNKNOWN);
  856. physical = 0; /* no block yet */
  857. }
  858. if (bmv->bmv_oflags & BMV_OF_LAST)
  859. fiemap_flags |= FIEMAP_EXTENT_LAST;
  860. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  861. length, fiemap_flags);
  862. if (error > 0) {
  863. error = 0;
  864. *full = 1; /* user array now full */
  865. }
  866. return -error;
  867. }
  868. STATIC int
  869. xfs_vn_fiemap(
  870. struct inode *inode,
  871. struct fiemap_extent_info *fieinfo,
  872. u64 start,
  873. u64 length)
  874. {
  875. xfs_inode_t *ip = XFS_I(inode);
  876. struct getbmapx bm;
  877. int error;
  878. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  879. if (error)
  880. return error;
  881. /* Set up bmap header for xfs internal routine */
  882. bm.bmv_offset = BTOBB(start);
  883. /* Special case for whole file */
  884. if (length == FIEMAP_MAX_OFFSET)
  885. bm.bmv_length = -1LL;
  886. else
  887. bm.bmv_length = BTOBB(length);
  888. /* We add one because in getbmap world count includes the header */
  889. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  890. fieinfo->fi_extents_max + 1;
  891. bm.bmv_count = min_t(__s32, bm.bmv_count,
  892. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  893. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  894. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  895. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  896. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  897. bm.bmv_iflags |= BMV_IF_DELALLOC;
  898. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  899. if (error)
  900. return -error;
  901. return 0;
  902. }
  903. static const struct inode_operations xfs_inode_operations = {
  904. .get_acl = xfs_get_acl,
  905. .getattr = xfs_vn_getattr,
  906. .setattr = xfs_vn_setattr,
  907. .setxattr = generic_setxattr,
  908. .getxattr = generic_getxattr,
  909. .removexattr = generic_removexattr,
  910. .listxattr = xfs_vn_listxattr,
  911. .fiemap = xfs_vn_fiemap,
  912. .update_time = xfs_vn_update_time,
  913. };
  914. static const struct inode_operations xfs_dir_inode_operations = {
  915. .create = xfs_vn_create,
  916. .lookup = xfs_vn_lookup,
  917. .link = xfs_vn_link,
  918. .unlink = xfs_vn_unlink,
  919. .symlink = xfs_vn_symlink,
  920. .mkdir = xfs_vn_mkdir,
  921. /*
  922. * Yes, XFS uses the same method for rmdir and unlink.
  923. *
  924. * There are some subtile differences deeper in the code,
  925. * but we use S_ISDIR to check for those.
  926. */
  927. .rmdir = xfs_vn_unlink,
  928. .mknod = xfs_vn_mknod,
  929. .rename = xfs_vn_rename,
  930. .get_acl = xfs_get_acl,
  931. .getattr = xfs_vn_getattr,
  932. .setattr = xfs_vn_setattr,
  933. .setxattr = generic_setxattr,
  934. .getxattr = generic_getxattr,
  935. .removexattr = generic_removexattr,
  936. .listxattr = xfs_vn_listxattr,
  937. .update_time = xfs_vn_update_time,
  938. };
  939. static const struct inode_operations xfs_dir_ci_inode_operations = {
  940. .create = xfs_vn_create,
  941. .lookup = xfs_vn_ci_lookup,
  942. .link = xfs_vn_link,
  943. .unlink = xfs_vn_unlink,
  944. .symlink = xfs_vn_symlink,
  945. .mkdir = xfs_vn_mkdir,
  946. /*
  947. * Yes, XFS uses the same method for rmdir and unlink.
  948. *
  949. * There are some subtile differences deeper in the code,
  950. * but we use S_ISDIR to check for those.
  951. */
  952. .rmdir = xfs_vn_unlink,
  953. .mknod = xfs_vn_mknod,
  954. .rename = xfs_vn_rename,
  955. .get_acl = xfs_get_acl,
  956. .getattr = xfs_vn_getattr,
  957. .setattr = xfs_vn_setattr,
  958. .setxattr = generic_setxattr,
  959. .getxattr = generic_getxattr,
  960. .removexattr = generic_removexattr,
  961. .listxattr = xfs_vn_listxattr,
  962. .update_time = xfs_vn_update_time,
  963. };
  964. static const struct inode_operations xfs_symlink_inode_operations = {
  965. .readlink = generic_readlink,
  966. .follow_link = xfs_vn_follow_link,
  967. .put_link = xfs_vn_put_link,
  968. .get_acl = xfs_get_acl,
  969. .getattr = xfs_vn_getattr,
  970. .setattr = xfs_vn_setattr,
  971. .setxattr = generic_setxattr,
  972. .getxattr = generic_getxattr,
  973. .removexattr = generic_removexattr,
  974. .listxattr = xfs_vn_listxattr,
  975. .update_time = xfs_vn_update_time,
  976. };
  977. STATIC void
  978. xfs_diflags_to_iflags(
  979. struct inode *inode,
  980. struct xfs_inode *ip)
  981. {
  982. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  983. inode->i_flags |= S_IMMUTABLE;
  984. else
  985. inode->i_flags &= ~S_IMMUTABLE;
  986. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  987. inode->i_flags |= S_APPEND;
  988. else
  989. inode->i_flags &= ~S_APPEND;
  990. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  991. inode->i_flags |= S_SYNC;
  992. else
  993. inode->i_flags &= ~S_SYNC;
  994. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  995. inode->i_flags |= S_NOATIME;
  996. else
  997. inode->i_flags &= ~S_NOATIME;
  998. }
  999. /*
  1000. * Initialize the Linux inode, set up the operation vectors and
  1001. * unlock the inode.
  1002. *
  1003. * When reading existing inodes from disk this is called directly
  1004. * from xfs_iget, when creating a new inode it is called from
  1005. * xfs_ialloc after setting up the inode.
  1006. *
  1007. * We are always called with an uninitialised linux inode here.
  1008. * We need to initialise the necessary fields and take a reference
  1009. * on it.
  1010. */
  1011. void
  1012. xfs_setup_inode(
  1013. struct xfs_inode *ip)
  1014. {
  1015. struct inode *inode = &ip->i_vnode;
  1016. inode->i_ino = ip->i_ino;
  1017. inode->i_state = I_NEW;
  1018. inode_sb_list_add(inode);
  1019. /* make the inode look hashed for the writeback code */
  1020. hlist_add_fake(&inode->i_hash);
  1021. inode->i_mode = ip->i_d.di_mode;
  1022. set_nlink(inode, ip->i_d.di_nlink);
  1023. inode->i_uid = ip->i_d.di_uid;
  1024. inode->i_gid = ip->i_d.di_gid;
  1025. switch (inode->i_mode & S_IFMT) {
  1026. case S_IFBLK:
  1027. case S_IFCHR:
  1028. inode->i_rdev =
  1029. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1030. sysv_minor(ip->i_df.if_u2.if_rdev));
  1031. break;
  1032. default:
  1033. inode->i_rdev = 0;
  1034. break;
  1035. }
  1036. inode->i_generation = ip->i_d.di_gen;
  1037. i_size_write(inode, ip->i_d.di_size);
  1038. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  1039. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  1040. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  1041. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  1042. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  1043. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  1044. xfs_diflags_to_iflags(inode, ip);
  1045. switch (inode->i_mode & S_IFMT) {
  1046. case S_IFREG:
  1047. inode->i_op = &xfs_inode_operations;
  1048. inode->i_fop = &xfs_file_operations;
  1049. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1050. break;
  1051. case S_IFDIR:
  1052. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1053. inode->i_op = &xfs_dir_ci_inode_operations;
  1054. else
  1055. inode->i_op = &xfs_dir_inode_operations;
  1056. inode->i_fop = &xfs_dir_file_operations;
  1057. break;
  1058. case S_IFLNK:
  1059. inode->i_op = &xfs_symlink_inode_operations;
  1060. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  1061. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1062. break;
  1063. default:
  1064. inode->i_op = &xfs_inode_operations;
  1065. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1066. break;
  1067. }
  1068. /*
  1069. * If there is no attribute fork no ACL can exist on this inode,
  1070. * and it can't have any file capabilities attached to it either.
  1071. */
  1072. if (!XFS_IFORK_Q(ip)) {
  1073. inode_has_no_xattr(inode);
  1074. cache_no_acl(inode);
  1075. }
  1076. xfs_iflags_clear(ip, XFS_INEW);
  1077. barrier();
  1078. unlock_new_inode(inode);
  1079. }