xfs_iops.c 31 KB

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