xfs_iops.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs_platform.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_acl.h"
  15. #include "xfs_quota.h"
  16. #include "xfs_da_format.h"
  17. #include "xfs_da_btree.h"
  18. #include "xfs_attr.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_trans_space.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_icache.h"
  24. #include "xfs_symlink.h"
  25. #include "xfs_dir2.h"
  26. #include "xfs_iomap.h"
  27. #include "xfs_error.h"
  28. #include "xfs_ioctl.h"
  29. #include "xfs_xattr.h"
  30. #include "xfs_file.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_zone_alloc.h"
  33. #include <linux/posix_acl.h>
  34. #include <linux/security.h>
  35. #include <linux/iversion.h>
  36. #include <linux/fiemap.h>
  37. /*
  38. * Directories have different lock order w.r.t. mmap_lock compared to regular
  39. * files. This is due to readdir potentially triggering page faults on a user
  40. * buffer inside filldir(), and this happens with the ilock on the directory
  41. * held. For regular files, the lock order is the other way around - the
  42. * mmap_lock is taken during the page fault, and then we lock the ilock to do
  43. * block mapping. Hence we need a different class for the directory ilock so
  44. * that lockdep can tell them apart. Directories in the metadata directory
  45. * tree get a separate class so that lockdep reports will warn us if someone
  46. * ever tries to lock regular directories after locking metadata directories.
  47. */
  48. static struct lock_class_key xfs_nondir_ilock_class;
  49. static struct lock_class_key xfs_dir_ilock_class;
  50. static int
  51. xfs_initxattrs(
  52. struct inode *inode,
  53. const struct xattr *xattr_array,
  54. void *fs_info)
  55. {
  56. const struct xattr *xattr;
  57. struct xfs_inode *ip = XFS_I(inode);
  58. int error = 0;
  59. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  60. struct xfs_da_args args = {
  61. .dp = ip,
  62. .attr_filter = XFS_ATTR_SECURE,
  63. .name = xattr->name,
  64. .namelen = strlen(xattr->name),
  65. .value = xattr->value,
  66. .valuelen = xattr->value_len,
  67. };
  68. error = xfs_attr_change(&args, XFS_ATTRUPDATE_UPSERT);
  69. if (error < 0)
  70. break;
  71. }
  72. return error;
  73. }
  74. /*
  75. * Hook in SELinux. This is not quite correct yet, what we really need
  76. * here (as we do for default ACLs) is a mechanism by which creation of
  77. * these attrs can be journalled at inode creation time (along with the
  78. * inode, of course, such that log replay can't cause these to be lost).
  79. */
  80. int
  81. xfs_inode_init_security(
  82. struct inode *inode,
  83. struct inode *dir,
  84. const struct qstr *qstr)
  85. {
  86. return security_inode_init_security(inode, dir, qstr,
  87. &xfs_initxattrs, NULL);
  88. }
  89. static void
  90. xfs_dentry_to_name(
  91. struct xfs_name *namep,
  92. struct dentry *dentry)
  93. {
  94. namep->name = dentry->d_name.name;
  95. namep->len = dentry->d_name.len;
  96. namep->type = XFS_DIR3_FT_UNKNOWN;
  97. }
  98. static int
  99. xfs_dentry_mode_to_name(
  100. struct xfs_name *namep,
  101. struct dentry *dentry,
  102. int mode)
  103. {
  104. namep->name = dentry->d_name.name;
  105. namep->len = dentry->d_name.len;
  106. namep->type = xfs_mode_to_ftype(mode);
  107. if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
  108. return -EFSCORRUPTED;
  109. return 0;
  110. }
  111. STATIC void
  112. xfs_cleanup_inode(
  113. struct inode *dir,
  114. struct inode *inode,
  115. struct dentry *dentry)
  116. {
  117. struct xfs_name teardown;
  118. /* Oh, the horror.
  119. * If we can't add the ACL or we fail in
  120. * xfs_inode_init_security we must back out.
  121. * ENOSPC can hit here, among other things.
  122. */
  123. xfs_dentry_to_name(&teardown, dentry);
  124. xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
  125. }
  126. /*
  127. * Check to see if we are likely to need an extended attribute to be added to
  128. * the inode we are about to allocate. This allows the attribute fork to be
  129. * created during the inode allocation, reducing the number of transactions we
  130. * need to do in this fast path.
  131. *
  132. * The security checks are optimistic, but not guaranteed. The two LSMs that
  133. * require xattrs to be added here (selinux and smack) are also the only two
  134. * LSMs that add a sb->s_security structure to the superblock. Hence if security
  135. * is enabled and sb->s_security is set, we have a pretty good idea that we are
  136. * going to be asked to add a security xattr immediately after allocating the
  137. * xfs inode and instantiating the VFS inode.
  138. */
  139. static inline bool
  140. xfs_create_need_xattr(
  141. struct inode *dir,
  142. struct posix_acl *default_acl,
  143. struct posix_acl *acl)
  144. {
  145. if (acl)
  146. return true;
  147. if (default_acl)
  148. return true;
  149. #if IS_ENABLED(CONFIG_SECURITY)
  150. if (dir->i_sb->s_security)
  151. return true;
  152. #endif
  153. return false;
  154. }
  155. STATIC int
  156. xfs_generic_create(
  157. struct mnt_idmap *idmap,
  158. struct inode *dir,
  159. struct dentry *dentry,
  160. umode_t mode,
  161. dev_t rdev,
  162. struct file *tmpfile) /* unnamed file */
  163. {
  164. struct xfs_icreate_args args = {
  165. .idmap = idmap,
  166. .pip = XFS_I(dir),
  167. .rdev = rdev,
  168. .mode = mode,
  169. };
  170. struct inode *inode;
  171. struct xfs_inode *ip = NULL;
  172. struct posix_acl *default_acl, *acl;
  173. struct xfs_name name;
  174. int error;
  175. /*
  176. * Irix uses Missed'em'V split, but doesn't want to see
  177. * the upper 5 bits of (14bit) major.
  178. */
  179. if (S_ISCHR(args.mode) || S_ISBLK(args.mode)) {
  180. if (unlikely(!sysv_valid_dev(args.rdev) ||
  181. MAJOR(args.rdev) & ~0x1ff))
  182. return -EINVAL;
  183. } else {
  184. args.rdev = 0;
  185. }
  186. error = posix_acl_create(dir, &args.mode, &default_acl, &acl);
  187. if (error)
  188. return error;
  189. /* Verify mode is valid also for tmpfile case */
  190. error = xfs_dentry_mode_to_name(&name, dentry, args.mode);
  191. if (unlikely(error))
  192. goto out_free_acl;
  193. if (!tmpfile) {
  194. if (xfs_create_need_xattr(dir, default_acl, acl))
  195. args.flags |= XFS_ICREATE_INIT_XATTRS;
  196. error = xfs_create(&args, &name, &ip);
  197. } else {
  198. args.flags |= XFS_ICREATE_TMPFILE;
  199. /*
  200. * If this temporary file will not be linkable, don't bother
  201. * creating an attr fork to receive a parent pointer.
  202. */
  203. if (tmpfile->f_flags & O_EXCL)
  204. args.flags |= XFS_ICREATE_UNLINKABLE;
  205. error = xfs_create_tmpfile(&args, &ip);
  206. }
  207. if (unlikely(error))
  208. goto out_free_acl;
  209. inode = VFS_I(ip);
  210. error = xfs_inode_init_security(inode, dir, &dentry->d_name);
  211. if (unlikely(error))
  212. goto out_cleanup_inode;
  213. if (default_acl) {
  214. error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  215. if (error)
  216. goto out_cleanup_inode;
  217. }
  218. if (acl) {
  219. error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
  220. if (error)
  221. goto out_cleanup_inode;
  222. }
  223. xfs_setup_iops(ip);
  224. if (tmpfile) {
  225. /*
  226. * The VFS requires that any inode fed to d_tmpfile must have
  227. * nlink == 1 so that it can decrement the nlink in d_tmpfile.
  228. * However, we created the temp file with nlink == 0 because
  229. * we're not allowed to put an inode with nlink > 0 on the
  230. * unlinked list. Therefore we have to set nlink to 1 so that
  231. * d_tmpfile can immediately set it back to zero.
  232. */
  233. set_nlink(inode, 1);
  234. d_tmpfile(tmpfile, inode);
  235. } else
  236. d_instantiate(dentry, inode);
  237. xfs_finish_inode_setup(ip);
  238. out_free_acl:
  239. posix_acl_release(default_acl);
  240. posix_acl_release(acl);
  241. return error;
  242. out_cleanup_inode:
  243. xfs_finish_inode_setup(ip);
  244. if (!tmpfile)
  245. xfs_cleanup_inode(dir, inode, dentry);
  246. xfs_irele(ip);
  247. goto out_free_acl;
  248. }
  249. STATIC int
  250. xfs_vn_mknod(
  251. struct mnt_idmap *idmap,
  252. struct inode *dir,
  253. struct dentry *dentry,
  254. umode_t mode,
  255. dev_t rdev)
  256. {
  257. return xfs_generic_create(idmap, dir, dentry, mode, rdev, NULL);
  258. }
  259. STATIC int
  260. xfs_vn_create(
  261. struct mnt_idmap *idmap,
  262. struct inode *dir,
  263. struct dentry *dentry,
  264. umode_t mode,
  265. bool flags)
  266. {
  267. return xfs_generic_create(idmap, dir, dentry, mode, 0, NULL);
  268. }
  269. STATIC struct dentry *
  270. xfs_vn_mkdir(
  271. struct mnt_idmap *idmap,
  272. struct inode *dir,
  273. struct dentry *dentry,
  274. umode_t mode)
  275. {
  276. return ERR_PTR(xfs_generic_create(idmap, dir, dentry, mode | S_IFDIR, 0, NULL));
  277. }
  278. STATIC struct dentry *
  279. xfs_vn_lookup(
  280. struct inode *dir,
  281. struct dentry *dentry,
  282. unsigned int flags)
  283. {
  284. struct inode *inode;
  285. struct xfs_inode *cip;
  286. struct xfs_name name;
  287. int error;
  288. if (dentry->d_name.len >= MAXNAMELEN)
  289. return ERR_PTR(-ENAMETOOLONG);
  290. xfs_dentry_to_name(&name, dentry);
  291. error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
  292. if (likely(!error))
  293. inode = VFS_I(cip);
  294. else if (likely(error == -ENOENT))
  295. inode = NULL;
  296. else
  297. inode = ERR_PTR(error);
  298. return d_splice_alias(inode, dentry);
  299. }
  300. STATIC struct dentry *
  301. xfs_vn_ci_lookup(
  302. struct inode *dir,
  303. struct dentry *dentry,
  304. unsigned int flags)
  305. {
  306. struct xfs_inode *ip;
  307. struct xfs_name xname;
  308. struct xfs_name ci_name;
  309. struct qstr dname;
  310. int error;
  311. if (dentry->d_name.len >= MAXNAMELEN)
  312. return ERR_PTR(-ENAMETOOLONG);
  313. xfs_dentry_to_name(&xname, dentry);
  314. error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
  315. if (unlikely(error)) {
  316. if (unlikely(error != -ENOENT))
  317. return ERR_PTR(error);
  318. /*
  319. * call d_add(dentry, NULL) here when d_drop_negative_children
  320. * is called in xfs_vn_mknod (ie. allow negative dentries
  321. * with CI filesystems).
  322. */
  323. return NULL;
  324. }
  325. /* if exact match, just splice and exit */
  326. if (!ci_name.name)
  327. return d_splice_alias(VFS_I(ip), dentry);
  328. /* else case-insensitive match... */
  329. dname.name = ci_name.name;
  330. dname.len = ci_name.len;
  331. dentry = d_add_ci(dentry, VFS_I(ip), &dname);
  332. kfree(ci_name.name);
  333. return dentry;
  334. }
  335. STATIC int
  336. xfs_vn_link(
  337. struct dentry *old_dentry,
  338. struct inode *dir,
  339. struct dentry *dentry)
  340. {
  341. struct inode *inode = d_inode(old_dentry);
  342. struct xfs_name name;
  343. int error;
  344. error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
  345. if (unlikely(error))
  346. return error;
  347. if (IS_PRIVATE(inode))
  348. return -EPERM;
  349. error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
  350. if (unlikely(error))
  351. return error;
  352. ihold(inode);
  353. d_instantiate(dentry, inode);
  354. return 0;
  355. }
  356. STATIC int
  357. xfs_vn_unlink(
  358. struct inode *dir,
  359. struct dentry *dentry)
  360. {
  361. struct xfs_name name;
  362. int error;
  363. xfs_dentry_to_name(&name, dentry);
  364. error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
  365. if (error)
  366. return error;
  367. /*
  368. * With unlink, the VFS makes the dentry "negative": no inode,
  369. * but still hashed. This is incompatible with case-insensitive
  370. * mode, so invalidate (unhash) the dentry in CI-mode.
  371. */
  372. if (xfs_has_asciici(XFS_M(dir->i_sb)))
  373. d_invalidate(dentry);
  374. return 0;
  375. }
  376. STATIC int
  377. xfs_vn_symlink(
  378. struct mnt_idmap *idmap,
  379. struct inode *dir,
  380. struct dentry *dentry,
  381. const char *symname)
  382. {
  383. struct inode *inode;
  384. struct xfs_inode *cip = NULL;
  385. struct xfs_name name;
  386. int error;
  387. umode_t mode = S_IFLNK | S_IRWXUGO;
  388. error = xfs_dentry_mode_to_name(&name, dentry, mode);
  389. if (unlikely(error))
  390. goto out;
  391. error = xfs_symlink(idmap, XFS_I(dir), &name, symname, mode, &cip);
  392. if (unlikely(error))
  393. goto out;
  394. inode = VFS_I(cip);
  395. error = xfs_inode_init_security(inode, dir, &dentry->d_name);
  396. if (unlikely(error))
  397. goto out_cleanup_inode;
  398. xfs_setup_iops(cip);
  399. d_instantiate(dentry, inode);
  400. xfs_finish_inode_setup(cip);
  401. return 0;
  402. out_cleanup_inode:
  403. xfs_finish_inode_setup(cip);
  404. xfs_cleanup_inode(dir, inode, dentry);
  405. xfs_irele(cip);
  406. out:
  407. return error;
  408. }
  409. STATIC int
  410. xfs_vn_rename(
  411. struct mnt_idmap *idmap,
  412. struct inode *odir,
  413. struct dentry *odentry,
  414. struct inode *ndir,
  415. struct dentry *ndentry,
  416. unsigned int flags)
  417. {
  418. struct inode *new_inode = d_inode(ndentry);
  419. int omode = 0;
  420. int error;
  421. struct xfs_name oname;
  422. struct xfs_name nname;
  423. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  424. return -EINVAL;
  425. /* if we are exchanging files, we need to set i_mode of both files */
  426. if (flags & RENAME_EXCHANGE)
  427. omode = d_inode(ndentry)->i_mode;
  428. error = xfs_dentry_mode_to_name(&oname, odentry, omode);
  429. if (omode && unlikely(error))
  430. return error;
  431. error = xfs_dentry_mode_to_name(&nname, ndentry,
  432. d_inode(odentry)->i_mode);
  433. if (unlikely(error))
  434. return error;
  435. return xfs_rename(idmap, XFS_I(odir), &oname,
  436. XFS_I(d_inode(odentry)), XFS_I(ndir), &nname,
  437. new_inode ? XFS_I(new_inode) : NULL, flags);
  438. }
  439. /*
  440. * careful here - this function can get called recursively, so
  441. * we need to be very careful about how much stack we use.
  442. * uio is kmalloced for this reason...
  443. */
  444. STATIC const char *
  445. xfs_vn_get_link(
  446. struct dentry *dentry,
  447. struct inode *inode,
  448. struct delayed_call *done)
  449. {
  450. char *link;
  451. int error = -ENOMEM;
  452. if (!dentry)
  453. return ERR_PTR(-ECHILD);
  454. link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
  455. if (!link)
  456. goto out_err;
  457. error = xfs_readlink(XFS_I(d_inode(dentry)), link);
  458. if (unlikely(error))
  459. goto out_kfree;
  460. set_delayed_call(done, kfree_link, link);
  461. return link;
  462. out_kfree:
  463. kfree(link);
  464. out_err:
  465. return ERR_PTR(error);
  466. }
  467. static uint32_t
  468. xfs_stat_blksize(
  469. struct xfs_inode *ip)
  470. {
  471. struct xfs_mount *mp = ip->i_mount;
  472. /*
  473. * If the file blocks are being allocated from a realtime volume, then
  474. * always return the realtime extent size.
  475. */
  476. if (XFS_IS_REALTIME_INODE(ip))
  477. return XFS_FSB_TO_B(mp, xfs_get_extsz_hint(ip) ? : 1);
  478. /*
  479. * Allow large block sizes to be reported to userspace programs if the
  480. * "largeio" mount option is used.
  481. *
  482. * If compatibility mode is specified, simply return the basic unit of
  483. * caching so that we don't get inefficient read/modify/write I/O from
  484. * user apps. Otherwise....
  485. *
  486. * If the underlying volume is a stripe, then return the stripe width in
  487. * bytes as the recommended I/O size. It is not a stripe and we've set a
  488. * default buffered I/O size, return that, otherwise return the compat
  489. * default.
  490. */
  491. if (xfs_has_large_iosize(mp)) {
  492. if (mp->m_swidth)
  493. return XFS_FSB_TO_B(mp, mp->m_swidth);
  494. if (xfs_has_allocsize(mp))
  495. return 1U << mp->m_allocsize_log;
  496. }
  497. return max_t(uint32_t, PAGE_SIZE, mp->m_sb.sb_blocksize);
  498. }
  499. static void
  500. xfs_report_dioalign(
  501. struct xfs_inode *ip,
  502. struct kstat *stat)
  503. {
  504. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  505. struct block_device *bdev = target->bt_bdev;
  506. stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN;
  507. stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
  508. /*
  509. * For COW inodes, we can only perform out of place writes of entire
  510. * allocation units (blocks or RT extents).
  511. * For writes smaller than the allocation unit, we must fall back to
  512. * buffered I/O to perform read-modify-write cycles. At best this is
  513. * highly inefficient; at worst it leads to page cache invalidation
  514. * races. Tell applications to avoid this by reporting the larger write
  515. * alignment in dio_offset_align, and the smaller read alignment in
  516. * dio_read_offset_align.
  517. */
  518. stat->dio_read_offset_align = bdev_logical_block_size(bdev);
  519. if (xfs_is_cow_inode(ip))
  520. stat->dio_offset_align = xfs_inode_alloc_unitsize(ip);
  521. else
  522. stat->dio_offset_align = stat->dio_read_offset_align;
  523. }
  524. unsigned int
  525. xfs_get_atomic_write_min(
  526. struct xfs_inode *ip)
  527. {
  528. struct xfs_mount *mp = ip->i_mount;
  529. /*
  530. * If we can complete an atomic write via atomic out of place writes,
  531. * then advertise a minimum size of one fsblock. Without this
  532. * mechanism, we can only guarantee atomic writes up to a single LBA.
  533. *
  534. * If out of place writes are not available, we can guarantee an atomic
  535. * write of exactly one single fsblock if the bdev will make that
  536. * guarantee for us.
  537. */
  538. if (xfs_inode_can_hw_atomic_write(ip) ||
  539. xfs_inode_can_sw_atomic_write(ip))
  540. return mp->m_sb.sb_blocksize;
  541. return 0;
  542. }
  543. unsigned int
  544. xfs_get_atomic_write_max(
  545. struct xfs_inode *ip)
  546. {
  547. struct xfs_mount *mp = ip->i_mount;
  548. /*
  549. * If out of place writes are not available, we can guarantee an atomic
  550. * write of exactly one single fsblock if the bdev will make that
  551. * guarantee for us.
  552. */
  553. if (!xfs_inode_can_sw_atomic_write(ip)) {
  554. if (xfs_inode_can_hw_atomic_write(ip))
  555. return mp->m_sb.sb_blocksize;
  556. return 0;
  557. }
  558. /*
  559. * If we can complete an atomic write via atomic out of place writes,
  560. * then advertise a maximum size of whatever we can complete through
  561. * that means. Hardware support is reported via max_opt, not here.
  562. */
  563. if (XFS_IS_REALTIME_INODE(ip))
  564. return XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_RTG].awu_max);
  565. return XFS_FSB_TO_B(mp, mp->m_groups[XG_TYPE_AG].awu_max);
  566. }
  567. unsigned int
  568. xfs_get_atomic_write_max_opt(
  569. struct xfs_inode *ip)
  570. {
  571. unsigned int awu_max = xfs_get_atomic_write_max(ip);
  572. /* if the max is 1x block, then just keep behaviour that opt is 0 */
  573. if (awu_max <= ip->i_mount->m_sb.sb_blocksize)
  574. return 0;
  575. /*
  576. * Advertise the maximum size of an atomic write that we can tell the
  577. * block device to perform for us. In general the bdev limit will be
  578. * less than our out of place write limit, but we don't want to exceed
  579. * the awu_max.
  580. */
  581. return min(awu_max, xfs_inode_buftarg(ip)->bt_awu_max);
  582. }
  583. static void
  584. xfs_report_atomic_write(
  585. struct xfs_inode *ip,
  586. struct kstat *stat)
  587. {
  588. generic_fill_statx_atomic_writes(stat,
  589. xfs_get_atomic_write_min(ip),
  590. xfs_get_atomic_write_max(ip),
  591. xfs_get_atomic_write_max_opt(ip));
  592. }
  593. STATIC int
  594. xfs_vn_getattr(
  595. struct mnt_idmap *idmap,
  596. const struct path *path,
  597. struct kstat *stat,
  598. u32 request_mask,
  599. unsigned int query_flags)
  600. {
  601. struct inode *inode = d_inode(path->dentry);
  602. struct xfs_inode *ip = XFS_I(inode);
  603. struct xfs_mount *mp = ip->i_mount;
  604. vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
  605. vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
  606. trace_xfs_getattr(ip);
  607. if (xfs_is_shutdown(mp))
  608. return -EIO;
  609. stat->size = XFS_ISIZE(ip);
  610. stat->dev = inode->i_sb->s_dev;
  611. stat->mode = inode->i_mode;
  612. stat->nlink = inode->i_nlink;
  613. stat->uid = vfsuid_into_kuid(vfsuid);
  614. stat->gid = vfsgid_into_kgid(vfsgid);
  615. stat->ino = ip->i_ino;
  616. stat->atime = inode_get_atime(inode);
  617. fill_mg_cmtime(stat, request_mask, inode);
  618. stat->blocks = XFS_FSB_TO_BB(mp, ip->i_nblocks + ip->i_delayed_blks);
  619. if (xfs_has_v3inodes(mp)) {
  620. if (request_mask & STATX_BTIME) {
  621. stat->result_mask |= STATX_BTIME;
  622. stat->btime = ip->i_crtime;
  623. }
  624. }
  625. /*
  626. * Note: If you add another clause to set an attribute flag, please
  627. * update attributes_mask below.
  628. */
  629. if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE)
  630. stat->attributes |= STATX_ATTR_IMMUTABLE;
  631. if (ip->i_diflags & XFS_DIFLAG_APPEND)
  632. stat->attributes |= STATX_ATTR_APPEND;
  633. if (ip->i_diflags & XFS_DIFLAG_NODUMP)
  634. stat->attributes |= STATX_ATTR_NODUMP;
  635. stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
  636. STATX_ATTR_APPEND |
  637. STATX_ATTR_NODUMP);
  638. switch (inode->i_mode & S_IFMT) {
  639. case S_IFBLK:
  640. case S_IFCHR:
  641. stat->blksize = BLKDEV_IOSIZE;
  642. stat->rdev = inode->i_rdev;
  643. break;
  644. case S_IFREG:
  645. if (request_mask & (STATX_DIOALIGN | STATX_DIO_READ_ALIGN))
  646. xfs_report_dioalign(ip, stat);
  647. if (request_mask & STATX_WRITE_ATOMIC)
  648. xfs_report_atomic_write(ip, stat);
  649. fallthrough;
  650. default:
  651. stat->blksize = xfs_stat_blksize(ip);
  652. stat->rdev = 0;
  653. break;
  654. }
  655. return 0;
  656. }
  657. static int
  658. xfs_vn_change_ok(
  659. struct mnt_idmap *idmap,
  660. struct dentry *dentry,
  661. struct iattr *iattr)
  662. {
  663. struct xfs_mount *mp = XFS_I(d_inode(dentry))->i_mount;
  664. if (xfs_is_readonly(mp))
  665. return -EROFS;
  666. if (xfs_is_shutdown(mp))
  667. return -EIO;
  668. return setattr_prepare(idmap, dentry, iattr);
  669. }
  670. /*
  671. * Set non-size attributes of an inode.
  672. *
  673. * Caution: The caller of this function is responsible for calling
  674. * setattr_prepare() or otherwise verifying the change is fine.
  675. */
  676. static int
  677. xfs_setattr_nonsize(
  678. struct mnt_idmap *idmap,
  679. struct dentry *dentry,
  680. struct xfs_inode *ip,
  681. struct iattr *iattr)
  682. {
  683. xfs_mount_t *mp = ip->i_mount;
  684. struct inode *inode = VFS_I(ip);
  685. int mask = iattr->ia_valid;
  686. xfs_trans_t *tp;
  687. int error;
  688. kuid_t uid = GLOBAL_ROOT_UID;
  689. kgid_t gid = GLOBAL_ROOT_GID;
  690. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  691. struct xfs_dquot *old_udqp = NULL, *old_gdqp = NULL;
  692. ASSERT((mask & ATTR_SIZE) == 0);
  693. /*
  694. * If disk quotas is on, we make sure that the dquots do exist on disk,
  695. * before we start any other transactions. Trying to do this later
  696. * is messy. We don't care to take a readlock to look at the ids
  697. * in inode here, because we can't hold it across the trans_reserve.
  698. * If the IDs do change before we take the ilock, we're covered
  699. * because the i_*dquot fields will get updated anyway.
  700. */
  701. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  702. uint qflags = 0;
  703. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  704. uid = from_vfsuid(idmap, i_user_ns(inode),
  705. iattr->ia_vfsuid);
  706. qflags |= XFS_QMOPT_UQUOTA;
  707. } else {
  708. uid = inode->i_uid;
  709. }
  710. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  711. gid = from_vfsgid(idmap, i_user_ns(inode),
  712. iattr->ia_vfsgid);
  713. qflags |= XFS_QMOPT_GQUOTA;
  714. } else {
  715. gid = inode->i_gid;
  716. }
  717. /*
  718. * We take a reference when we initialize udqp and gdqp,
  719. * so it is important that we never blindly double trip on
  720. * the same variable. See xfs_create() for an example.
  721. */
  722. ASSERT(udqp == NULL);
  723. ASSERT(gdqp == NULL);
  724. error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_projid,
  725. qflags, &udqp, &gdqp, NULL);
  726. if (error)
  727. return error;
  728. }
  729. error = xfs_trans_alloc_ichange(ip, udqp, gdqp, NULL,
  730. has_capability_noaudit(current, CAP_FOWNER), &tp);
  731. if (error)
  732. goto out_dqrele;
  733. /*
  734. * Register quota modifications in the transaction. Must be the owner
  735. * or privileged. These IDs could have changed since we last looked at
  736. * them. But, we're assured that if the ownership did change while we
  737. * didn't have the inode locked, inode's dquot(s) would have changed
  738. * also.
  739. */
  740. if (XFS_IS_UQUOTA_ON(mp) &&
  741. i_uid_needs_update(idmap, iattr, inode)) {
  742. ASSERT(udqp);
  743. old_udqp = xfs_qm_vop_chown(tp, ip, &ip->i_udquot, udqp);
  744. }
  745. if (XFS_IS_GQUOTA_ON(mp) &&
  746. i_gid_needs_update(idmap, iattr, inode)) {
  747. ASSERT(xfs_has_pquotino(mp) || !XFS_IS_PQUOTA_ON(mp));
  748. ASSERT(gdqp);
  749. old_gdqp = xfs_qm_vop_chown(tp, ip, &ip->i_gdquot, gdqp);
  750. }
  751. setattr_copy(idmap, inode, iattr);
  752. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  753. XFS_STATS_INC(mp, xs_ig_attrchg);
  754. if (xfs_has_wsync(mp))
  755. xfs_trans_set_sync(tp);
  756. error = xfs_trans_commit(tp);
  757. /*
  758. * Release any dquot(s) the inode had kept before chown.
  759. */
  760. xfs_qm_dqrele(old_udqp);
  761. xfs_qm_dqrele(old_gdqp);
  762. xfs_qm_dqrele(udqp);
  763. xfs_qm_dqrele(gdqp);
  764. if (error)
  765. return error;
  766. /*
  767. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  768. * update. We could avoid this with linked transactions
  769. * and passing down the transaction pointer all the way
  770. * to attr_set. No previous user of the generic
  771. * Posix ACL code seems to care about this issue either.
  772. */
  773. if (mask & ATTR_MODE) {
  774. error = posix_acl_chmod(idmap, dentry, inode->i_mode);
  775. if (error)
  776. return error;
  777. }
  778. return 0;
  779. out_dqrele:
  780. xfs_qm_dqrele(udqp);
  781. xfs_qm_dqrele(gdqp);
  782. return error;
  783. }
  784. /*
  785. * Truncate file. Must have write permission and not be a directory.
  786. *
  787. * Caution: The caller of this function is responsible for calling
  788. * setattr_prepare() or otherwise verifying the change is fine.
  789. */
  790. STATIC int
  791. xfs_setattr_size(
  792. struct mnt_idmap *idmap,
  793. struct dentry *dentry,
  794. struct xfs_inode *ip,
  795. struct iattr *iattr)
  796. {
  797. struct xfs_mount *mp = ip->i_mount;
  798. struct inode *inode = VFS_I(ip);
  799. xfs_off_t oldsize, newsize;
  800. struct xfs_trans *tp;
  801. int error;
  802. uint lock_flags = 0;
  803. uint resblks = 0;
  804. bool did_zeroing = false;
  805. struct xfs_zone_alloc_ctx ac = { };
  806. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
  807. ASSERT(S_ISREG(inode->i_mode));
  808. ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
  809. ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
  810. oldsize = inode->i_size;
  811. newsize = iattr->ia_size;
  812. /*
  813. * Short circuit the truncate case for zero length files.
  814. */
  815. if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
  816. if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
  817. return 0;
  818. /*
  819. * Use the regular setattr path to update the timestamps.
  820. */
  821. iattr->ia_valid &= ~ATTR_SIZE;
  822. return xfs_setattr_nonsize(idmap, dentry, ip, iattr);
  823. }
  824. /*
  825. * Make sure that the dquots are attached to the inode.
  826. */
  827. error = xfs_qm_dqattach(ip);
  828. if (error)
  829. return error;
  830. /*
  831. * Wait for all direct I/O to complete.
  832. */
  833. inode_dio_wait(inode);
  834. /*
  835. * Normally xfs_zoned_space_reserve is supposed to be called outside the
  836. * IOLOCK. For truncate we can't do that since ->setattr is called with
  837. * it already held by the VFS. So for now chicken out and try to
  838. * allocate space under it.
  839. *
  840. * To avoid deadlocks this means we can't block waiting for space, which
  841. * can lead to spurious -ENOSPC if there are no directly available
  842. * blocks. We mitigate this a bit by allowing zeroing to dip into the
  843. * reserved pool, but eventually the VFS calling convention needs to
  844. * change.
  845. */
  846. if (xfs_is_zoned_inode(ip)) {
  847. error = xfs_zoned_space_reserve(mp, 1,
  848. XFS_ZR_NOWAIT | XFS_ZR_RESERVED, &ac);
  849. if (error) {
  850. if (error == -EAGAIN)
  851. return -ENOSPC;
  852. return error;
  853. }
  854. }
  855. /*
  856. * File data changes must be complete before we start the transaction to
  857. * modify the inode. This needs to be done before joining the inode to
  858. * the transaction because the inode cannot be unlocked once it is a
  859. * part of the transaction.
  860. *
  861. * Start with zeroing any data beyond EOF that we may expose on file
  862. * extension, or zeroing out the rest of the block on a downward
  863. * truncate.
  864. */
  865. if (newsize > oldsize) {
  866. trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
  867. error = xfs_zero_range(ip, oldsize, newsize - oldsize,
  868. &ac, &did_zeroing);
  869. } else {
  870. error = xfs_truncate_page(ip, newsize, &ac, &did_zeroing);
  871. }
  872. if (xfs_is_zoned_inode(ip))
  873. xfs_zoned_space_unreserve(mp, &ac);
  874. if (error)
  875. return error;
  876. /*
  877. * We've already locked out new page faults, so now we can safely remove
  878. * pages from the page cache knowing they won't get refaulted until we
  879. * drop the XFS_MMAP_EXCL lock after the extent manipulations are
  880. * complete. The truncate_setsize() call also cleans partial EOF page
  881. * PTEs on extending truncates and hence ensures sub-page block size
  882. * filesystems are correctly handled, too.
  883. *
  884. * We have to do all the page cache truncate work outside the
  885. * transaction context as the "lock" order is page lock->log space
  886. * reservation as defined by extent allocation in the writeback path.
  887. * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
  888. * having already truncated the in-memory version of the file (i.e. made
  889. * user visible changes). There's not much we can do about this, except
  890. * to hope that the caller sees ENOMEM and retries the truncate
  891. * operation.
  892. *
  893. * And we update in-core i_size and truncate page cache beyond newsize
  894. * before writeback the [i_disk_size, newsize] range, so we're
  895. * guaranteed not to write stale data past the new EOF on truncate down.
  896. */
  897. truncate_setsize(inode, newsize);
  898. /*
  899. * We are going to log the inode size change in this transaction so
  900. * any previous writes that are beyond the on disk EOF and the new
  901. * EOF that have not been written out need to be written here. If we
  902. * do not write the data out, we expose ourselves to the null files
  903. * problem. Note that this includes any block zeroing we did above;
  904. * otherwise those blocks may not be zeroed after a crash.
  905. */
  906. if (did_zeroing ||
  907. (newsize > ip->i_disk_size && oldsize != ip->i_disk_size)) {
  908. error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  909. ip->i_disk_size, newsize - 1);
  910. if (error)
  911. return error;
  912. }
  913. /*
  914. * For realtime inode with more than one block rtextsize, we need the
  915. * block reservation for bmap btree block allocations/splits that can
  916. * happen since it could split the tail written extent and convert the
  917. * right beyond EOF one to unwritten.
  918. */
  919. if (xfs_inode_has_bigrtalloc(ip))
  920. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  921. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks,
  922. 0, 0, &tp);
  923. if (error)
  924. return error;
  925. lock_flags |= XFS_ILOCK_EXCL;
  926. xfs_ilock(ip, XFS_ILOCK_EXCL);
  927. xfs_trans_ijoin(tp, ip, 0);
  928. /*
  929. * Only change the c/mtime if we are changing the size or we are
  930. * explicitly asked to change it. This handles the semantic difference
  931. * between truncate() and ftruncate() as implemented in the VFS.
  932. *
  933. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  934. * special case where we need to update the times despite not having
  935. * these flags set. For all other operations the VFS set these flags
  936. * explicitly if it wants a timestamp update.
  937. */
  938. if (newsize != oldsize &&
  939. !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
  940. iattr->ia_ctime = iattr->ia_mtime =
  941. current_time(inode);
  942. iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  943. }
  944. /*
  945. * The first thing we do is set the size to new_size permanently on
  946. * disk. This way we don't have to worry about anyone ever being able
  947. * to look at the data being freed even in the face of a crash.
  948. * What we're getting around here is the case where we free a block, it
  949. * is allocated to another file, it is written to, and then we crash.
  950. * If the new data gets written to the file but the log buffers
  951. * containing the free and reallocation don't, then we'd end up with
  952. * garbage in the blocks being freed. As long as we make the new size
  953. * permanent before actually freeing any blocks it doesn't matter if
  954. * they get written to.
  955. */
  956. ip->i_disk_size = newsize;
  957. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  958. if (newsize <= oldsize) {
  959. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
  960. if (error)
  961. goto out_trans_cancel;
  962. /*
  963. * Truncated "down", so we're removing references to old data
  964. * here - if we delay flushing for a long time, we expose
  965. * ourselves unduly to the notorious NULL files problem. So,
  966. * we mark this inode and flush it when the file is closed,
  967. * and do not wait the usual (long) time for writeout.
  968. */
  969. xfs_iflags_set(ip, XFS_ITRUNCATED);
  970. /* A truncate down always removes post-EOF blocks. */
  971. xfs_inode_clear_eofblocks_tag(ip);
  972. }
  973. ASSERT(!(iattr->ia_valid & (ATTR_UID | ATTR_GID)));
  974. setattr_copy(idmap, inode, iattr);
  975. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  976. XFS_STATS_INC(mp, xs_ig_attrchg);
  977. if (xfs_has_wsync(mp))
  978. xfs_trans_set_sync(tp);
  979. error = xfs_trans_commit(tp);
  980. out_unlock:
  981. if (lock_flags)
  982. xfs_iunlock(ip, lock_flags);
  983. return error;
  984. out_trans_cancel:
  985. xfs_trans_cancel(tp);
  986. goto out_unlock;
  987. }
  988. int
  989. xfs_vn_setattr_size(
  990. struct mnt_idmap *idmap,
  991. struct dentry *dentry,
  992. struct iattr *iattr)
  993. {
  994. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  995. int error;
  996. trace_xfs_setattr(ip);
  997. error = xfs_vn_change_ok(idmap, dentry, iattr);
  998. if (error)
  999. return error;
  1000. return xfs_setattr_size(idmap, dentry, ip, iattr);
  1001. }
  1002. STATIC int
  1003. xfs_vn_setattr(
  1004. struct mnt_idmap *idmap,
  1005. struct dentry *dentry,
  1006. struct iattr *iattr)
  1007. {
  1008. struct inode *inode = d_inode(dentry);
  1009. struct xfs_inode *ip = XFS_I(inode);
  1010. int error;
  1011. if (iattr->ia_valid & ATTR_SIZE) {
  1012. uint iolock;
  1013. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  1014. iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  1015. error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
  1016. if (error) {
  1017. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  1018. return error;
  1019. }
  1020. error = xfs_vn_setattr_size(idmap, dentry, iattr);
  1021. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  1022. } else {
  1023. trace_xfs_setattr(ip);
  1024. error = xfs_vn_change_ok(idmap, dentry, iattr);
  1025. if (!error)
  1026. error = xfs_setattr_nonsize(idmap, dentry, ip, iattr);
  1027. }
  1028. return error;
  1029. }
  1030. STATIC int
  1031. xfs_vn_update_time(
  1032. struct inode *inode,
  1033. enum fs_update_time type,
  1034. unsigned int flags)
  1035. {
  1036. struct xfs_inode *ip = XFS_I(inode);
  1037. struct xfs_mount *mp = ip->i_mount;
  1038. int log_flags = XFS_ILOG_TIMESTAMP;
  1039. struct xfs_trans *tp;
  1040. int error;
  1041. trace_xfs_update_time(ip);
  1042. if (inode->i_sb->s_flags & SB_LAZYTIME) {
  1043. int dirty;
  1044. dirty = inode_update_time(inode, type, flags);
  1045. if (dirty <= 0)
  1046. return dirty;
  1047. if (dirty == I_DIRTY_TIME) {
  1048. __mark_inode_dirty(inode, I_DIRTY_TIME);
  1049. return 0;
  1050. }
  1051. /* Capture the iversion update that just occurred */
  1052. log_flags |= XFS_ILOG_CORE;
  1053. } else {
  1054. if (flags & IOCB_NOWAIT)
  1055. return -EAGAIN;
  1056. }
  1057. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  1058. if (error)
  1059. return error;
  1060. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1061. if (type == FS_UPD_ATIME)
  1062. inode_set_atime_to_ts(inode, current_time(inode));
  1063. else
  1064. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  1065. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1066. xfs_trans_log_inode(tp, ip, log_flags);
  1067. return xfs_trans_commit(tp);
  1068. }
  1069. static void
  1070. xfs_vn_sync_lazytime(
  1071. struct inode *inode)
  1072. {
  1073. struct xfs_inode *ip = XFS_I(inode);
  1074. struct xfs_mount *mp = ip->i_mount;
  1075. struct xfs_trans *tp;
  1076. if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))
  1077. return;
  1078. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1079. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1080. xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
  1081. xfs_trans_commit(tp);
  1082. }
  1083. STATIC int
  1084. xfs_vn_fiemap(
  1085. struct inode *inode,
  1086. struct fiemap_extent_info *fieinfo,
  1087. u64 start,
  1088. u64 length)
  1089. {
  1090. int error;
  1091. xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
  1092. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  1093. fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
  1094. error = iomap_fiemap(inode, fieinfo, start, length,
  1095. &xfs_xattr_iomap_ops);
  1096. } else {
  1097. error = iomap_fiemap(inode, fieinfo, start, length,
  1098. &xfs_read_iomap_ops);
  1099. }
  1100. xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
  1101. return error;
  1102. }
  1103. STATIC int
  1104. xfs_vn_tmpfile(
  1105. struct mnt_idmap *idmap,
  1106. struct inode *dir,
  1107. struct file *file,
  1108. umode_t mode)
  1109. {
  1110. int err = xfs_generic_create(idmap, dir, file->f_path.dentry, mode, 0, file);
  1111. return finish_open_simple(file, err);
  1112. }
  1113. static const struct inode_operations xfs_inode_operations = {
  1114. .get_inode_acl = xfs_get_acl,
  1115. .set_acl = xfs_set_acl,
  1116. .getattr = xfs_vn_getattr,
  1117. .setattr = xfs_vn_setattr,
  1118. .listxattr = xfs_vn_listxattr,
  1119. .fiemap = xfs_vn_fiemap,
  1120. .update_time = xfs_vn_update_time,
  1121. .sync_lazytime = xfs_vn_sync_lazytime,
  1122. .fileattr_get = xfs_fileattr_get,
  1123. .fileattr_set = xfs_fileattr_set,
  1124. };
  1125. static const struct inode_operations xfs_dir_inode_operations = {
  1126. .create = xfs_vn_create,
  1127. .lookup = xfs_vn_lookup,
  1128. .link = xfs_vn_link,
  1129. .unlink = xfs_vn_unlink,
  1130. .symlink = xfs_vn_symlink,
  1131. .mkdir = xfs_vn_mkdir,
  1132. /*
  1133. * Yes, XFS uses the same method for rmdir and unlink.
  1134. *
  1135. * There are some subtile differences deeper in the code,
  1136. * but we use S_ISDIR to check for those.
  1137. */
  1138. .rmdir = xfs_vn_unlink,
  1139. .mknod = xfs_vn_mknod,
  1140. .rename = xfs_vn_rename,
  1141. .get_inode_acl = xfs_get_acl,
  1142. .set_acl = xfs_set_acl,
  1143. .getattr = xfs_vn_getattr,
  1144. .setattr = xfs_vn_setattr,
  1145. .listxattr = xfs_vn_listxattr,
  1146. .update_time = xfs_vn_update_time,
  1147. .sync_lazytime = xfs_vn_sync_lazytime,
  1148. .tmpfile = xfs_vn_tmpfile,
  1149. .fileattr_get = xfs_fileattr_get,
  1150. .fileattr_set = xfs_fileattr_set,
  1151. };
  1152. static const struct inode_operations xfs_dir_ci_inode_operations = {
  1153. .create = xfs_vn_create,
  1154. .lookup = xfs_vn_ci_lookup,
  1155. .link = xfs_vn_link,
  1156. .unlink = xfs_vn_unlink,
  1157. .symlink = xfs_vn_symlink,
  1158. .mkdir = xfs_vn_mkdir,
  1159. /*
  1160. * Yes, XFS uses the same method for rmdir and unlink.
  1161. *
  1162. * There are some subtile differences deeper in the code,
  1163. * but we use S_ISDIR to check for those.
  1164. */
  1165. .rmdir = xfs_vn_unlink,
  1166. .mknod = xfs_vn_mknod,
  1167. .rename = xfs_vn_rename,
  1168. .get_inode_acl = xfs_get_acl,
  1169. .set_acl = xfs_set_acl,
  1170. .getattr = xfs_vn_getattr,
  1171. .setattr = xfs_vn_setattr,
  1172. .listxattr = xfs_vn_listxattr,
  1173. .update_time = xfs_vn_update_time,
  1174. .sync_lazytime = xfs_vn_sync_lazytime,
  1175. .tmpfile = xfs_vn_tmpfile,
  1176. .fileattr_get = xfs_fileattr_get,
  1177. .fileattr_set = xfs_fileattr_set,
  1178. };
  1179. static const struct inode_operations xfs_symlink_inode_operations = {
  1180. .get_link = xfs_vn_get_link,
  1181. .getattr = xfs_vn_getattr,
  1182. .setattr = xfs_vn_setattr,
  1183. .listxattr = xfs_vn_listxattr,
  1184. .update_time = xfs_vn_update_time,
  1185. .sync_lazytime = xfs_vn_sync_lazytime,
  1186. .fileattr_get = xfs_fileattr_get,
  1187. .fileattr_set = xfs_fileattr_set,
  1188. };
  1189. /* Figure out if this file actually supports DAX. */
  1190. static bool
  1191. xfs_inode_supports_dax(
  1192. struct xfs_inode *ip)
  1193. {
  1194. struct xfs_mount *mp = ip->i_mount;
  1195. /* Only supported on regular files. */
  1196. if (!S_ISREG(VFS_I(ip)->i_mode))
  1197. return false;
  1198. /* Block size must match page size */
  1199. if (mp->m_sb.sb_blocksize != PAGE_SIZE)
  1200. return false;
  1201. /* Device has to support DAX too. */
  1202. return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
  1203. }
  1204. static bool
  1205. xfs_inode_should_enable_dax(
  1206. struct xfs_inode *ip)
  1207. {
  1208. if (!IS_ENABLED(CONFIG_FS_DAX))
  1209. return false;
  1210. if (xfs_has_dax_never(ip->i_mount))
  1211. return false;
  1212. if (!xfs_inode_supports_dax(ip))
  1213. return false;
  1214. if (xfs_has_dax_always(ip->i_mount))
  1215. return true;
  1216. if (ip->i_diflags2 & XFS_DIFLAG2_DAX)
  1217. return true;
  1218. return false;
  1219. }
  1220. void
  1221. xfs_diflags_to_iflags(
  1222. struct xfs_inode *ip,
  1223. bool init)
  1224. {
  1225. struct inode *inode = VFS_I(ip);
  1226. unsigned int xflags = xfs_ip2xflags(ip);
  1227. unsigned int flags = 0;
  1228. ASSERT(!(IS_DAX(inode) && init));
  1229. if (xflags & FS_XFLAG_IMMUTABLE)
  1230. flags |= S_IMMUTABLE;
  1231. if (xflags & FS_XFLAG_APPEND)
  1232. flags |= S_APPEND;
  1233. if (xflags & FS_XFLAG_SYNC)
  1234. flags |= S_SYNC;
  1235. if (xflags & FS_XFLAG_NOATIME)
  1236. flags |= S_NOATIME;
  1237. if (init && xfs_inode_should_enable_dax(ip))
  1238. flags |= S_DAX;
  1239. /*
  1240. * S_DAX can only be set during inode initialization and is never set by
  1241. * the VFS, so we cannot mask off S_DAX in i_flags.
  1242. */
  1243. inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
  1244. inode->i_flags |= flags;
  1245. }
  1246. /*
  1247. * Initialize the Linux inode.
  1248. *
  1249. * When reading existing inodes from disk this is called directly from xfs_iget,
  1250. * when creating a new inode it is called from xfs_init_new_inode after setting
  1251. * up the inode. These callers have different criteria for clearing XFS_INEW, so
  1252. * leave it up to the caller to deal with unlocking the inode appropriately.
  1253. */
  1254. void
  1255. xfs_setup_inode(
  1256. struct xfs_inode *ip)
  1257. {
  1258. struct inode *inode = &ip->i_vnode;
  1259. gfp_t gfp_mask;
  1260. bool is_meta = xfs_is_internal_inode(ip);
  1261. inode->i_ino = ip->i_ino;
  1262. inode_state_set_raw(inode, I_NEW);
  1263. inode_sb_list_add(inode);
  1264. /* make the inode look hashed for the writeback code */
  1265. inode_fake_hash(inode);
  1266. i_size_write(inode, ip->i_disk_size);
  1267. xfs_diflags_to_iflags(ip, true);
  1268. /*
  1269. * Mark our metadata files as private so that LSMs and the ACL code
  1270. * don't try to add their own metadata or reason about these files,
  1271. * and users cannot ever obtain file handles to them.
  1272. */
  1273. if (is_meta) {
  1274. inode->i_flags |= S_PRIVATE;
  1275. inode->i_opflags &= ~IOP_XATTR;
  1276. }
  1277. if (S_ISDIR(inode->i_mode)) {
  1278. /*
  1279. * We set the i_rwsem class here to avoid potential races with
  1280. * lockdep_annotate_inode_mutex_key() reinitialising the lock
  1281. * after a filehandle lookup has already found the inode in
  1282. * cache before it has been unlocked via unlock_new_inode().
  1283. */
  1284. lockdep_set_class(&inode->i_rwsem,
  1285. &inode->i_sb->s_type->i_mutex_dir_key);
  1286. lockdep_set_class(&ip->i_lock, &xfs_dir_ilock_class);
  1287. } else {
  1288. lockdep_set_class(&ip->i_lock, &xfs_nondir_ilock_class);
  1289. }
  1290. /*
  1291. * Ensure all page cache allocations are done from GFP_NOFS context to
  1292. * prevent direct reclaim recursion back into the filesystem and blowing
  1293. * stacks or deadlocking.
  1294. */
  1295. gfp_mask = mapping_gfp_mask(inode->i_mapping);
  1296. mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
  1297. /*
  1298. * For real-time inodes update the stable write flags to that of the RT
  1299. * device instead of the data device.
  1300. */
  1301. if (S_ISREG(inode->i_mode) && XFS_IS_REALTIME_INODE(ip))
  1302. xfs_update_stable_writes(ip);
  1303. /*
  1304. * If there is no attribute fork no ACL can exist on this inode,
  1305. * and it can't have any file capabilities attached to it either.
  1306. */
  1307. if (!xfs_inode_has_attr_fork(ip)) {
  1308. inode_has_no_xattr(inode);
  1309. cache_no_acl(inode);
  1310. }
  1311. }
  1312. void
  1313. xfs_setup_iops(
  1314. struct xfs_inode *ip)
  1315. {
  1316. struct inode *inode = &ip->i_vnode;
  1317. switch (inode->i_mode & S_IFMT) {
  1318. case S_IFREG:
  1319. inode->i_op = &xfs_inode_operations;
  1320. inode->i_fop = &xfs_file_operations;
  1321. if (IS_DAX(inode))
  1322. inode->i_mapping->a_ops = &xfs_dax_aops;
  1323. else
  1324. inode->i_mapping->a_ops = &xfs_address_space_operations;
  1325. break;
  1326. case S_IFDIR:
  1327. if (xfs_has_asciici(XFS_M(inode->i_sb)))
  1328. inode->i_op = &xfs_dir_ci_inode_operations;
  1329. else
  1330. inode->i_op = &xfs_dir_inode_operations;
  1331. inode->i_fop = &xfs_dir_file_operations;
  1332. break;
  1333. case S_IFLNK:
  1334. inode->i_op = &xfs_symlink_inode_operations;
  1335. break;
  1336. default:
  1337. inode->i_op = &xfs_inode_operations;
  1338. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1339. break;
  1340. }
  1341. }