xfs_iops.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. int
  395. xfs_setattr_nonsize(
  396. struct xfs_inode *ip,
  397. struct iattr *iattr,
  398. int flags)
  399. {
  400. xfs_mount_t *mp = ip->i_mount;
  401. struct inode *inode = VFS_I(ip);
  402. int mask = iattr->ia_valid;
  403. xfs_trans_t *tp;
  404. int error;
  405. uid_t uid = 0, iuid = 0;
  406. gid_t gid = 0, igid = 0;
  407. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  408. struct xfs_dquot *olddquot1 = NULL, *olddquot2 = NULL;
  409. trace_xfs_setattr(ip);
  410. if (mp->m_flags & XFS_MOUNT_RDONLY)
  411. return XFS_ERROR(EROFS);
  412. if (XFS_FORCED_SHUTDOWN(mp))
  413. return XFS_ERROR(EIO);
  414. error = -inode_change_ok(inode, iattr);
  415. if (error)
  416. return XFS_ERROR(error);
  417. ASSERT((mask & ATTR_SIZE) == 0);
  418. /*
  419. * If disk quotas is on, we make sure that the dquots do exist on disk,
  420. * before we start any other transactions. Trying to do this later
  421. * is messy. We don't care to take a readlock to look at the ids
  422. * in inode here, because we can't hold it across the trans_reserve.
  423. * If the IDs do change before we take the ilock, we're covered
  424. * because the i_*dquot fields will get updated anyway.
  425. */
  426. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  427. uint qflags = 0;
  428. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  429. uid = iattr->ia_uid;
  430. qflags |= XFS_QMOPT_UQUOTA;
  431. } else {
  432. uid = ip->i_d.di_uid;
  433. }
  434. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  435. gid = iattr->ia_gid;
  436. qflags |= XFS_QMOPT_GQUOTA;
  437. } else {
  438. gid = ip->i_d.di_gid;
  439. }
  440. /*
  441. * We take a reference when we initialize udqp and gdqp,
  442. * so it is important that we never blindly double trip on
  443. * the same variable. See xfs_create() for an example.
  444. */
  445. ASSERT(udqp == NULL);
  446. ASSERT(gdqp == NULL);
  447. error = xfs_qm_vop_dqalloc(ip, uid, gid, xfs_get_projid(ip),
  448. qflags, &udqp, &gdqp);
  449. if (error)
  450. return error;
  451. }
  452. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  453. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  454. if (error)
  455. goto out_dqrele;
  456. xfs_ilock(ip, XFS_ILOCK_EXCL);
  457. /*
  458. * Change file ownership. Must be the owner or privileged.
  459. */
  460. if (mask & (ATTR_UID|ATTR_GID)) {
  461. /*
  462. * These IDs could have changed since we last looked at them.
  463. * But, we're assured that if the ownership did change
  464. * while we didn't have the inode locked, inode's dquot(s)
  465. * would have changed also.
  466. */
  467. iuid = ip->i_d.di_uid;
  468. igid = ip->i_d.di_gid;
  469. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  470. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  471. /*
  472. * Do a quota reservation only if uid/gid is actually
  473. * going to change.
  474. */
  475. if (XFS_IS_QUOTA_RUNNING(mp) &&
  476. ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  477. (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
  478. ASSERT(tp);
  479. error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  480. capable(CAP_FOWNER) ?
  481. XFS_QMOPT_FORCE_RES : 0);
  482. if (error) /* out of quota */
  483. goto out_trans_cancel;
  484. }
  485. }
  486. xfs_trans_ijoin(tp, ip, 0);
  487. /*
  488. * Change file ownership. Must be the owner or privileged.
  489. */
  490. if (mask & (ATTR_UID|ATTR_GID)) {
  491. /*
  492. * CAP_FSETID overrides the following restrictions:
  493. *
  494. * The set-user-ID and set-group-ID bits of a file will be
  495. * cleared upon successful return from chown()
  496. */
  497. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  498. !capable(CAP_FSETID))
  499. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  500. /*
  501. * Change the ownerships and register quota modifications
  502. * in the transaction.
  503. */
  504. if (iuid != uid) {
  505. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  506. ASSERT(mask & ATTR_UID);
  507. ASSERT(udqp);
  508. olddquot1 = xfs_qm_vop_chown(tp, ip,
  509. &ip->i_udquot, udqp);
  510. }
  511. ip->i_d.di_uid = uid;
  512. inode->i_uid = uid;
  513. }
  514. if (igid != gid) {
  515. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  516. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  517. ASSERT(mask & ATTR_GID);
  518. ASSERT(gdqp);
  519. olddquot2 = xfs_qm_vop_chown(tp, ip,
  520. &ip->i_gdquot, gdqp);
  521. }
  522. ip->i_d.di_gid = gid;
  523. inode->i_gid = gid;
  524. }
  525. }
  526. /*
  527. * Change file access modes.
  528. */
  529. if (mask & ATTR_MODE) {
  530. umode_t mode = iattr->ia_mode;
  531. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  532. mode &= ~S_ISGID;
  533. ip->i_d.di_mode &= S_IFMT;
  534. ip->i_d.di_mode |= mode & ~S_IFMT;
  535. inode->i_mode &= S_IFMT;
  536. inode->i_mode |= mode & ~S_IFMT;
  537. }
  538. /*
  539. * Change file access or modified times.
  540. */
  541. if (mask & ATTR_ATIME) {
  542. inode->i_atime = iattr->ia_atime;
  543. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  544. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  545. }
  546. if (mask & ATTR_CTIME) {
  547. inode->i_ctime = iattr->ia_ctime;
  548. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  549. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  550. }
  551. if (mask & ATTR_MTIME) {
  552. inode->i_mtime = iattr->ia_mtime;
  553. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  554. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  555. }
  556. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  557. XFS_STATS_INC(xs_ig_attrchg);
  558. if (mp->m_flags & XFS_MOUNT_WSYNC)
  559. xfs_trans_set_sync(tp);
  560. error = xfs_trans_commit(tp, 0);
  561. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  562. /*
  563. * Release any dquot(s) the inode had kept before chown.
  564. */
  565. xfs_qm_dqrele(olddquot1);
  566. xfs_qm_dqrele(olddquot2);
  567. xfs_qm_dqrele(udqp);
  568. xfs_qm_dqrele(gdqp);
  569. if (error)
  570. return XFS_ERROR(error);
  571. /*
  572. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  573. * update. We could avoid this with linked transactions
  574. * and passing down the transaction pointer all the way
  575. * to attr_set. No previous user of the generic
  576. * Posix ACL code seems to care about this issue either.
  577. */
  578. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  579. error = -xfs_acl_chmod(inode);
  580. if (error)
  581. return XFS_ERROR(error);
  582. }
  583. return 0;
  584. out_trans_cancel:
  585. xfs_trans_cancel(tp, 0);
  586. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  587. out_dqrele:
  588. xfs_qm_dqrele(udqp);
  589. xfs_qm_dqrele(gdqp);
  590. return error;
  591. }
  592. /*
  593. * Truncate file. Must have write permission and not be a directory.
  594. */
  595. int
  596. xfs_setattr_size(
  597. struct xfs_inode *ip,
  598. struct iattr *iattr,
  599. int flags)
  600. {
  601. struct xfs_mount *mp = ip->i_mount;
  602. struct inode *inode = VFS_I(ip);
  603. int mask = iattr->ia_valid;
  604. xfs_off_t oldsize, newsize;
  605. struct xfs_trans *tp;
  606. int error;
  607. uint lock_flags = 0;
  608. uint commit_flags = 0;
  609. trace_xfs_setattr(ip);
  610. if (mp->m_flags & XFS_MOUNT_RDONLY)
  611. return XFS_ERROR(EROFS);
  612. if (XFS_FORCED_SHUTDOWN(mp))
  613. return XFS_ERROR(EIO);
  614. error = -inode_change_ok(inode, iattr);
  615. if (error)
  616. return XFS_ERROR(error);
  617. ASSERT(S_ISREG(ip->i_d.di_mode));
  618. ASSERT((mask & (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  619. ATTR_MTIME_SET|ATTR_KILL_SUID|ATTR_KILL_SGID|
  620. ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
  621. if (!(flags & XFS_ATTR_NOLOCK)) {
  622. lock_flags |= XFS_IOLOCK_EXCL;
  623. xfs_ilock(ip, lock_flags);
  624. }
  625. oldsize = inode->i_size;
  626. newsize = iattr->ia_size;
  627. /*
  628. * Short circuit the truncate case for zero length files.
  629. */
  630. if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
  631. if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
  632. goto out_unlock;
  633. /*
  634. * Use the regular setattr path to update the timestamps.
  635. */
  636. xfs_iunlock(ip, lock_flags);
  637. iattr->ia_valid &= ~ATTR_SIZE;
  638. return xfs_setattr_nonsize(ip, iattr, 0);
  639. }
  640. /*
  641. * Make sure that the dquots are attached to the inode.
  642. */
  643. error = xfs_qm_dqattach(ip, 0);
  644. if (error)
  645. goto out_unlock;
  646. /*
  647. * Now we can make the changes. Before we join the inode to the
  648. * transaction, take care of the part of the truncation that must be
  649. * done without the inode lock. This needs to be done before joining
  650. * the inode to the transaction, because the inode cannot be unlocked
  651. * once it is a part of the transaction.
  652. */
  653. if (newsize > oldsize) {
  654. /*
  655. * Do the first part of growing a file: zero any data in the
  656. * last block that is beyond the old EOF. We need to do this
  657. * before the inode is joined to the transaction to modify
  658. * i_size.
  659. */
  660. error = xfs_zero_eof(ip, newsize, oldsize);
  661. if (error)
  662. goto out_unlock;
  663. }
  664. /*
  665. * We are going to log the inode size change in this transaction so
  666. * any previous writes that are beyond the on disk EOF and the new
  667. * EOF that have not been written out need to be written here. If we
  668. * do not write the data out, we expose ourselves to the null files
  669. * problem.
  670. *
  671. * Only flush from the on disk size to the smaller of the in memory
  672. * file size or the new size as that's the range we really care about
  673. * here and prevents waiting for other data not within the range we
  674. * care about here.
  675. */
  676. if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
  677. error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  678. ip->i_d.di_size, newsize);
  679. if (error)
  680. goto out_unlock;
  681. }
  682. /*
  683. * Wait for all direct I/O to complete.
  684. */
  685. inode_dio_wait(inode);
  686. error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
  687. if (error)
  688. goto out_unlock;
  689. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  690. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  691. XFS_TRANS_PERM_LOG_RES,
  692. XFS_ITRUNCATE_LOG_COUNT);
  693. if (error)
  694. goto out_trans_cancel;
  695. truncate_setsize(inode, newsize);
  696. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  697. lock_flags |= XFS_ILOCK_EXCL;
  698. xfs_ilock(ip, XFS_ILOCK_EXCL);
  699. xfs_trans_ijoin(tp, ip, 0);
  700. /*
  701. * Only change the c/mtime if we are changing the size or we are
  702. * explicitly asked to change it. This handles the semantic difference
  703. * between truncate() and ftruncate() as implemented in the VFS.
  704. *
  705. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  706. * special case where we need to update the times despite not having
  707. * these flags set. For all other operations the VFS set these flags
  708. * explicitly if it wants a timestamp update.
  709. */
  710. if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
  711. iattr->ia_ctime = iattr->ia_mtime =
  712. current_fs_time(inode->i_sb);
  713. mask |= ATTR_CTIME | ATTR_MTIME;
  714. }
  715. /*
  716. * The first thing we do is set the size to new_size permanently on
  717. * disk. This way we don't have to worry about anyone ever being able
  718. * to look at the data being freed even in the face of a crash.
  719. * What we're getting around here is the case where we free a block, it
  720. * is allocated to another file, it is written to, and then we crash.
  721. * If the new data gets written to the file but the log buffers
  722. * containing the free and reallocation don't, then we'd end up with
  723. * garbage in the blocks being freed. As long as we make the new size
  724. * permanent before actually freeing any blocks it doesn't matter if
  725. * they get written to.
  726. */
  727. ip->i_d.di_size = newsize;
  728. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  729. if (newsize <= oldsize) {
  730. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  731. if (error)
  732. goto out_trans_abort;
  733. /*
  734. * Truncated "down", so we're removing references to old data
  735. * here - if we delay flushing for a long time, we expose
  736. * ourselves unduly to the notorious NULL files problem. So,
  737. * we mark this inode and flush it when the file is closed,
  738. * and do not wait the usual (long) time for writeout.
  739. */
  740. xfs_iflags_set(ip, XFS_ITRUNCATED);
  741. /* A truncate down always removes post-EOF blocks. */
  742. xfs_inode_clear_eofblocks_tag(ip);
  743. }
  744. if (mask & ATTR_CTIME) {
  745. inode->i_ctime = iattr->ia_ctime;
  746. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  747. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  748. }
  749. if (mask & ATTR_MTIME) {
  750. inode->i_mtime = iattr->ia_mtime;
  751. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  752. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  753. }
  754. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  755. XFS_STATS_INC(xs_ig_attrchg);
  756. if (mp->m_flags & XFS_MOUNT_WSYNC)
  757. xfs_trans_set_sync(tp);
  758. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  759. out_unlock:
  760. if (lock_flags)
  761. xfs_iunlock(ip, lock_flags);
  762. return error;
  763. out_trans_abort:
  764. commit_flags |= XFS_TRANS_ABORT;
  765. out_trans_cancel:
  766. xfs_trans_cancel(tp, commit_flags);
  767. goto out_unlock;
  768. }
  769. STATIC int
  770. xfs_vn_setattr(
  771. struct dentry *dentry,
  772. struct iattr *iattr)
  773. {
  774. if (iattr->ia_valid & ATTR_SIZE)
  775. return -xfs_setattr_size(XFS_I(dentry->d_inode), iattr, 0);
  776. return -xfs_setattr_nonsize(XFS_I(dentry->d_inode), iattr, 0);
  777. }
  778. STATIC int
  779. xfs_vn_update_time(
  780. struct inode *inode,
  781. struct timespec *now,
  782. int flags)
  783. {
  784. struct xfs_inode *ip = XFS_I(inode);
  785. struct xfs_mount *mp = ip->i_mount;
  786. struct xfs_trans *tp;
  787. int error;
  788. trace_xfs_update_time(ip);
  789. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  790. error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  791. if (error) {
  792. xfs_trans_cancel(tp, 0);
  793. return -error;
  794. }
  795. xfs_ilock(ip, XFS_ILOCK_EXCL);
  796. if (flags & S_CTIME) {
  797. inode->i_ctime = *now;
  798. ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
  799. ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
  800. }
  801. if (flags & S_MTIME) {
  802. inode->i_mtime = *now;
  803. ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
  804. ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
  805. }
  806. if (flags & S_ATIME) {
  807. inode->i_atime = *now;
  808. ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
  809. ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
  810. }
  811. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  812. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  813. return -xfs_trans_commit(tp, 0);
  814. }
  815. #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  816. /*
  817. * Call fiemap helper to fill in user data.
  818. * Returns positive errors to xfs_getbmap.
  819. */
  820. STATIC int
  821. xfs_fiemap_format(
  822. void **arg,
  823. struct getbmapx *bmv,
  824. int *full)
  825. {
  826. int error;
  827. struct fiemap_extent_info *fieinfo = *arg;
  828. u32 fiemap_flags = 0;
  829. u64 logical, physical, length;
  830. /* Do nothing for a hole */
  831. if (bmv->bmv_block == -1LL)
  832. return 0;
  833. logical = BBTOB(bmv->bmv_offset);
  834. physical = BBTOB(bmv->bmv_block);
  835. length = BBTOB(bmv->bmv_length);
  836. if (bmv->bmv_oflags & BMV_OF_PREALLOC)
  837. fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
  838. else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
  839. fiemap_flags |= FIEMAP_EXTENT_DELALLOC;
  840. physical = 0; /* no block yet */
  841. }
  842. if (bmv->bmv_oflags & BMV_OF_LAST)
  843. fiemap_flags |= FIEMAP_EXTENT_LAST;
  844. error = fiemap_fill_next_extent(fieinfo, logical, physical,
  845. length, fiemap_flags);
  846. if (error > 0) {
  847. error = 0;
  848. *full = 1; /* user array now full */
  849. }
  850. return -error;
  851. }
  852. STATIC int
  853. xfs_vn_fiemap(
  854. struct inode *inode,
  855. struct fiemap_extent_info *fieinfo,
  856. u64 start,
  857. u64 length)
  858. {
  859. xfs_inode_t *ip = XFS_I(inode);
  860. struct getbmapx bm;
  861. int error;
  862. error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
  863. if (error)
  864. return error;
  865. /* Set up bmap header for xfs internal routine */
  866. bm.bmv_offset = BTOBB(start);
  867. /* Special case for whole file */
  868. if (length == FIEMAP_MAX_OFFSET)
  869. bm.bmv_length = -1LL;
  870. else
  871. bm.bmv_length = BTOBB(length);
  872. /* We add one because in getbmap world count includes the header */
  873. bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
  874. fieinfo->fi_extents_max + 1;
  875. bm.bmv_count = min_t(__s32, bm.bmv_count,
  876. (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
  877. bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
  878. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
  879. bm.bmv_iflags |= BMV_IF_ATTRFORK;
  880. if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
  881. bm.bmv_iflags |= BMV_IF_DELALLOC;
  882. error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
  883. if (error)
  884. return -error;
  885. return 0;
  886. }
  887. static const struct inode_operations xfs_inode_operations = {
  888. .get_acl = xfs_get_acl,
  889. .getattr = xfs_vn_getattr,
  890. .setattr = xfs_vn_setattr,
  891. .setxattr = generic_setxattr,
  892. .getxattr = generic_getxattr,
  893. .removexattr = generic_removexattr,
  894. .listxattr = xfs_vn_listxattr,
  895. .fiemap = xfs_vn_fiemap,
  896. .update_time = xfs_vn_update_time,
  897. };
  898. static const struct inode_operations xfs_dir_inode_operations = {
  899. .create = xfs_vn_create,
  900. .lookup = xfs_vn_lookup,
  901. .link = xfs_vn_link,
  902. .unlink = xfs_vn_unlink,
  903. .symlink = xfs_vn_symlink,
  904. .mkdir = xfs_vn_mkdir,
  905. /*
  906. * Yes, XFS uses the same method for rmdir and unlink.
  907. *
  908. * There are some subtile differences deeper in the code,
  909. * but we use S_ISDIR to check for those.
  910. */
  911. .rmdir = xfs_vn_unlink,
  912. .mknod = xfs_vn_mknod,
  913. .rename = xfs_vn_rename,
  914. .get_acl = xfs_get_acl,
  915. .getattr = xfs_vn_getattr,
  916. .setattr = xfs_vn_setattr,
  917. .setxattr = generic_setxattr,
  918. .getxattr = generic_getxattr,
  919. .removexattr = generic_removexattr,
  920. .listxattr = xfs_vn_listxattr,
  921. .update_time = xfs_vn_update_time,
  922. };
  923. static const struct inode_operations xfs_dir_ci_inode_operations = {
  924. .create = xfs_vn_create,
  925. .lookup = xfs_vn_ci_lookup,
  926. .link = xfs_vn_link,
  927. .unlink = xfs_vn_unlink,
  928. .symlink = xfs_vn_symlink,
  929. .mkdir = xfs_vn_mkdir,
  930. /*
  931. * Yes, XFS uses the same method for rmdir and unlink.
  932. *
  933. * There are some subtile differences deeper in the code,
  934. * but we use S_ISDIR to check for those.
  935. */
  936. .rmdir = xfs_vn_unlink,
  937. .mknod = xfs_vn_mknod,
  938. .rename = xfs_vn_rename,
  939. .get_acl = xfs_get_acl,
  940. .getattr = xfs_vn_getattr,
  941. .setattr = xfs_vn_setattr,
  942. .setxattr = generic_setxattr,
  943. .getxattr = generic_getxattr,
  944. .removexattr = generic_removexattr,
  945. .listxattr = xfs_vn_listxattr,
  946. .update_time = xfs_vn_update_time,
  947. };
  948. static const struct inode_operations xfs_symlink_inode_operations = {
  949. .readlink = generic_readlink,
  950. .follow_link = xfs_vn_follow_link,
  951. .put_link = xfs_vn_put_link,
  952. .get_acl = xfs_get_acl,
  953. .getattr = xfs_vn_getattr,
  954. .setattr = xfs_vn_setattr,
  955. .setxattr = generic_setxattr,
  956. .getxattr = generic_getxattr,
  957. .removexattr = generic_removexattr,
  958. .listxattr = xfs_vn_listxattr,
  959. .update_time = xfs_vn_update_time,
  960. };
  961. STATIC void
  962. xfs_diflags_to_iflags(
  963. struct inode *inode,
  964. struct xfs_inode *ip)
  965. {
  966. if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
  967. inode->i_flags |= S_IMMUTABLE;
  968. else
  969. inode->i_flags &= ~S_IMMUTABLE;
  970. if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
  971. inode->i_flags |= S_APPEND;
  972. else
  973. inode->i_flags &= ~S_APPEND;
  974. if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
  975. inode->i_flags |= S_SYNC;
  976. else
  977. inode->i_flags &= ~S_SYNC;
  978. if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
  979. inode->i_flags |= S_NOATIME;
  980. else
  981. inode->i_flags &= ~S_NOATIME;
  982. }
  983. /*
  984. * Initialize the Linux inode, set up the operation vectors and
  985. * unlock the inode.
  986. *
  987. * When reading existing inodes from disk this is called directly
  988. * from xfs_iget, when creating a new inode it is called from
  989. * xfs_ialloc after setting up the inode.
  990. *
  991. * We are always called with an uninitialised linux inode here.
  992. * We need to initialise the necessary fields and take a reference
  993. * on it.
  994. */
  995. void
  996. xfs_setup_inode(
  997. struct xfs_inode *ip)
  998. {
  999. struct inode *inode = &ip->i_vnode;
  1000. inode->i_ino = ip->i_ino;
  1001. inode->i_state = I_NEW;
  1002. inode_sb_list_add(inode);
  1003. /* make the inode look hashed for the writeback code */
  1004. hlist_add_fake(&inode->i_hash);
  1005. inode->i_mode = ip->i_d.di_mode;
  1006. set_nlink(inode, ip->i_d.di_nlink);
  1007. inode->i_uid = ip->i_d.di_uid;
  1008. inode->i_gid = ip->i_d.di_gid;
  1009. switch (inode->i_mode & S_IFMT) {
  1010. case S_IFBLK:
  1011. case S_IFCHR:
  1012. inode->i_rdev =
  1013. MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
  1014. sysv_minor(ip->i_df.if_u2.if_rdev));
  1015. break;
  1016. default:
  1017. inode->i_rdev = 0;
  1018. break;
  1019. }
  1020. inode->i_generation = ip->i_d.di_gen;
  1021. i_size_write(inode, ip->i_d.di_size);
  1022. inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
  1023. inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  1024. inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  1025. inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  1026. inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  1027. inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  1028. xfs_diflags_to_iflags(inode, ip);
  1029. switch (inode->i_mode & S_IFMT) {
  1030. case S_IFREG:
  1031. inode->i_op = &xfs_inode_operations;
  1032. inode->i_fop = &xfs_file_operations;
  1033. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1034. break;
  1035. case S_IFDIR:
  1036. if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
  1037. inode->i_op = &xfs_dir_ci_inode_operations;
  1038. else
  1039. inode->i_op = &xfs_dir_inode_operations;
  1040. inode->i_fop = &xfs_dir_file_operations;
  1041. break;
  1042. case S_IFLNK:
  1043. inode->i_op = &xfs_symlink_inode_operations;
  1044. if (!(ip->i_df.if_flags & XFS_IFINLINE))
  1045. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1046. break;
  1047. default:
  1048. inode->i_op = &xfs_inode_operations;
  1049. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1050. break;
  1051. }
  1052. /*
  1053. * If there is no attribute fork no ACL can exist on this inode,
  1054. * and it can't have any file capabilities attached to it either.
  1055. */
  1056. if (!XFS_IFORK_Q(ip)) {
  1057. inode_has_no_xattr(inode);
  1058. cache_no_acl(inode);
  1059. }
  1060. xfs_iflags_clear(ip, XFS_INEW);
  1061. barrier();
  1062. unlock_new_inode(inode);
  1063. }