xfs_attr.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.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_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_da_format.h"
  16. #include "xfs_da_btree.h"
  17. #include "xfs_attr_sf.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_alloc.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_inode_item.h"
  22. #include "xfs_bmap.h"
  23. #include "xfs_bmap_util.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_attr.h"
  26. #include "xfs_attr_leaf.h"
  27. #include "xfs_attr_remote.h"
  28. #include "xfs_error.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_trans_space.h"
  31. #include "xfs_trace.h"
  32. /*
  33. * xfs_attr.c
  34. *
  35. * Provide the external interfaces to manage attribute lists.
  36. */
  37. /*========================================================================
  38. * Function prototypes for the kernel.
  39. *========================================================================*/
  40. /*
  41. * Internal routines when attribute list fits inside the inode.
  42. */
  43. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  44. /*
  45. * Internal routines when attribute list is one block.
  46. */
  47. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  48. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  49. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  50. /*
  51. * Internal routines when attribute list is more than one block.
  52. */
  53. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  54. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  55. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  56. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  57. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  58. STATIC int
  59. xfs_attr_args_init(
  60. struct xfs_da_args *args,
  61. struct xfs_inode *dp,
  62. const unsigned char *name,
  63. int flags)
  64. {
  65. if (!name)
  66. return -EINVAL;
  67. memset(args, 0, sizeof(*args));
  68. args->geo = dp->i_mount->m_attr_geo;
  69. args->whichfork = XFS_ATTR_FORK;
  70. args->dp = dp;
  71. args->flags = flags;
  72. args->name = name;
  73. args->namelen = strlen((const char *)name);
  74. if (args->namelen >= MAXNAMELEN)
  75. return -EFAULT; /* match IRIX behaviour */
  76. args->hashval = xfs_da_hashname(args->name, args->namelen);
  77. return 0;
  78. }
  79. int
  80. xfs_inode_hasattr(
  81. struct xfs_inode *ip)
  82. {
  83. if (!XFS_IFORK_Q(ip) ||
  84. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  85. ip->i_d.di_anextents == 0))
  86. return 0;
  87. return 1;
  88. }
  89. /*========================================================================
  90. * Overall external interface routines.
  91. *========================================================================*/
  92. /* Retrieve an extended attribute and its value. Must have ilock. */
  93. int
  94. xfs_attr_get_ilocked(
  95. struct xfs_inode *ip,
  96. struct xfs_da_args *args)
  97. {
  98. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
  99. if (!xfs_inode_hasattr(ip))
  100. return -ENOATTR;
  101. else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
  102. return xfs_attr_shortform_getvalue(args);
  103. else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
  104. return xfs_attr_leaf_get(args);
  105. else
  106. return xfs_attr_node_get(args);
  107. }
  108. /* Retrieve an extended attribute by name, and its value. */
  109. int
  110. xfs_attr_get(
  111. struct xfs_inode *ip,
  112. const unsigned char *name,
  113. unsigned char *value,
  114. int *valuelenp,
  115. int flags)
  116. {
  117. struct xfs_da_args args;
  118. uint lock_mode;
  119. int error;
  120. XFS_STATS_INC(ip->i_mount, xs_attr_get);
  121. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  122. return -EIO;
  123. error = xfs_attr_args_init(&args, ip, name, flags);
  124. if (error)
  125. return error;
  126. args.value = value;
  127. args.valuelen = *valuelenp;
  128. /* Entirely possible to look up a name which doesn't exist */
  129. args.op_flags = XFS_DA_OP_OKNOENT;
  130. lock_mode = xfs_ilock_attr_map_shared(ip);
  131. error = xfs_attr_get_ilocked(ip, &args);
  132. xfs_iunlock(ip, lock_mode);
  133. *valuelenp = args.valuelen;
  134. return error == -EEXIST ? 0 : error;
  135. }
  136. /*
  137. * Calculate how many blocks we need for the new attribute,
  138. */
  139. STATIC int
  140. xfs_attr_calc_size(
  141. struct xfs_da_args *args,
  142. int *local)
  143. {
  144. struct xfs_mount *mp = args->dp->i_mount;
  145. int size;
  146. int nblks;
  147. /*
  148. * Determine space new attribute will use, and if it would be
  149. * "local" or "remote" (note: local != inline).
  150. */
  151. size = xfs_attr_leaf_newentsize(args, local);
  152. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  153. if (*local) {
  154. if (size > (args->geo->blksize / 2)) {
  155. /* Double split possible */
  156. nblks *= 2;
  157. }
  158. } else {
  159. /*
  160. * Out of line attribute, cannot double split, but
  161. * make room for the attribute value itself.
  162. */
  163. uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
  164. nblks += dblocks;
  165. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  166. }
  167. return nblks;
  168. }
  169. STATIC int
  170. xfs_attr_try_sf_addname(
  171. struct xfs_inode *dp,
  172. struct xfs_da_args *args)
  173. {
  174. struct xfs_mount *mp = dp->i_mount;
  175. int error, error2;
  176. error = xfs_attr_shortform_addname(args);
  177. if (error == -ENOSPC)
  178. return error;
  179. /*
  180. * Commit the shortform mods, and we're done.
  181. * NOTE: this is also the error path (EEXIST, etc).
  182. */
  183. if (!error && (args->flags & ATTR_KERNOTIME) == 0)
  184. xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
  185. if (mp->m_flags & XFS_MOUNT_WSYNC)
  186. xfs_trans_set_sync(args->trans);
  187. error2 = xfs_trans_commit(args->trans);
  188. args->trans = NULL;
  189. return error ? error : error2;
  190. }
  191. /*
  192. * Set the attribute specified in @args.
  193. */
  194. int
  195. xfs_attr_set_args(
  196. struct xfs_da_args *args,
  197. struct xfs_buf **leaf_bp)
  198. {
  199. struct xfs_inode *dp = args->dp;
  200. int error;
  201. /*
  202. * If the attribute list is non-existent or a shortform list,
  203. * upgrade it to a single-leaf-block attribute list.
  204. */
  205. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
  206. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  207. dp->i_d.di_anextents == 0)) {
  208. /*
  209. * Build initial attribute list (if required).
  210. */
  211. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  212. xfs_attr_shortform_create(args);
  213. /*
  214. * Try to add the attr to the attribute list in the inode.
  215. */
  216. error = xfs_attr_try_sf_addname(dp, args);
  217. if (error != -ENOSPC)
  218. return error;
  219. /*
  220. * It won't fit in the shortform, transform to a leaf block.
  221. * GROT: another possible req'mt for a double-split btree op.
  222. */
  223. error = xfs_attr_shortform_to_leaf(args, leaf_bp);
  224. if (error)
  225. return error;
  226. /*
  227. * Prevent the leaf buffer from being unlocked so that a
  228. * concurrent AIL push cannot grab the half-baked leaf
  229. * buffer and run into problems with the write verifier.
  230. */
  231. xfs_trans_bhold(args->trans, *leaf_bp);
  232. error = xfs_defer_finish(&args->trans);
  233. if (error)
  234. return error;
  235. /*
  236. * Commit the leaf transformation. We'll need another
  237. * (linked) transaction to add the new attribute to the
  238. * leaf.
  239. */
  240. error = xfs_trans_roll_inode(&args->trans, dp);
  241. if (error)
  242. return error;
  243. xfs_trans_bjoin(args->trans, *leaf_bp);
  244. *leaf_bp = NULL;
  245. }
  246. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
  247. error = xfs_attr_leaf_addname(args);
  248. else
  249. error = xfs_attr_node_addname(args);
  250. return error;
  251. }
  252. /*
  253. * Remove the attribute specified in @args.
  254. */
  255. int
  256. xfs_attr_remove_args(
  257. struct xfs_da_args *args)
  258. {
  259. struct xfs_inode *dp = args->dp;
  260. int error;
  261. if (!xfs_inode_hasattr(dp)) {
  262. error = -ENOATTR;
  263. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  264. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  265. error = xfs_attr_shortform_remove(args);
  266. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  267. error = xfs_attr_leaf_removename(args);
  268. } else {
  269. error = xfs_attr_node_removename(args);
  270. }
  271. return error;
  272. }
  273. int
  274. xfs_attr_set(
  275. struct xfs_inode *dp,
  276. const unsigned char *name,
  277. unsigned char *value,
  278. int valuelen,
  279. int flags)
  280. {
  281. struct xfs_mount *mp = dp->i_mount;
  282. struct xfs_buf *leaf_bp = NULL;
  283. struct xfs_da_args args;
  284. struct xfs_trans_res tres;
  285. int rsvd = (flags & ATTR_ROOT) != 0;
  286. int error, local;
  287. XFS_STATS_INC(mp, xs_attr_set);
  288. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  289. return -EIO;
  290. error = xfs_attr_args_init(&args, dp, name, flags);
  291. if (error)
  292. return error;
  293. args.value = value;
  294. args.valuelen = valuelen;
  295. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  296. args.total = xfs_attr_calc_size(&args, &local);
  297. error = xfs_qm_dqattach(dp);
  298. if (error)
  299. return error;
  300. /*
  301. * If the inode doesn't have an attribute fork, add one.
  302. * (inode must not be locked when we call this routine)
  303. */
  304. if (XFS_IFORK_Q(dp) == 0) {
  305. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  306. XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
  307. error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
  308. if (error)
  309. return error;
  310. }
  311. tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
  312. M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
  313. tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
  314. tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  315. /*
  316. * Root fork attributes can use reserved data blocks for this
  317. * operation if necessary
  318. */
  319. error = xfs_trans_alloc(mp, &tres, args.total, 0,
  320. rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
  321. if (error)
  322. return error;
  323. xfs_ilock(dp, XFS_ILOCK_EXCL);
  324. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  325. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  326. XFS_QMOPT_RES_REGBLKS);
  327. if (error)
  328. goto out_trans_cancel;
  329. xfs_trans_ijoin(args.trans, dp, 0);
  330. error = xfs_attr_set_args(&args, &leaf_bp);
  331. if (error)
  332. goto out_release_leaf;
  333. if (!args.trans) {
  334. /* shortform attribute has already been committed */
  335. goto out_unlock;
  336. }
  337. /*
  338. * If this is a synchronous mount, make sure that the
  339. * transaction goes to disk before returning to the user.
  340. */
  341. if (mp->m_flags & XFS_MOUNT_WSYNC)
  342. xfs_trans_set_sync(args.trans);
  343. if ((flags & ATTR_KERNOTIME) == 0)
  344. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  345. /*
  346. * Commit the last in the sequence of transactions.
  347. */
  348. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  349. error = xfs_trans_commit(args.trans);
  350. out_unlock:
  351. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  352. return error;
  353. out_release_leaf:
  354. if (leaf_bp)
  355. xfs_trans_brelse(args.trans, leaf_bp);
  356. out_trans_cancel:
  357. if (args.trans)
  358. xfs_trans_cancel(args.trans);
  359. goto out_unlock;
  360. }
  361. /*
  362. * Generic handler routine to remove a name from an attribute list.
  363. * Transitions attribute list from Btree to shortform as necessary.
  364. */
  365. int
  366. xfs_attr_remove(
  367. struct xfs_inode *dp,
  368. const unsigned char *name,
  369. int flags)
  370. {
  371. struct xfs_mount *mp = dp->i_mount;
  372. struct xfs_da_args args;
  373. int error;
  374. XFS_STATS_INC(mp, xs_attr_remove);
  375. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  376. return -EIO;
  377. error = xfs_attr_args_init(&args, dp, name, flags);
  378. if (error)
  379. return error;
  380. /*
  381. * we have no control over the attribute names that userspace passes us
  382. * to remove, so we have to allow the name lookup prior to attribute
  383. * removal to fail.
  384. */
  385. args.op_flags = XFS_DA_OP_OKNOENT;
  386. error = xfs_qm_dqattach(dp);
  387. if (error)
  388. return error;
  389. /*
  390. * Root fork attributes can use reserved data blocks for this
  391. * operation if necessary
  392. */
  393. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
  394. XFS_ATTRRM_SPACE_RES(mp), 0,
  395. (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
  396. &args.trans);
  397. if (error)
  398. return error;
  399. xfs_ilock(dp, XFS_ILOCK_EXCL);
  400. /*
  401. * No need to make quota reservations here. We expect to release some
  402. * blocks not allocate in the common case.
  403. */
  404. xfs_trans_ijoin(args.trans, dp, 0);
  405. error = xfs_attr_remove_args(&args);
  406. if (error)
  407. goto out;
  408. /*
  409. * If this is a synchronous mount, make sure that the
  410. * transaction goes to disk before returning to the user.
  411. */
  412. if (mp->m_flags & XFS_MOUNT_WSYNC)
  413. xfs_trans_set_sync(args.trans);
  414. if ((flags & ATTR_KERNOTIME) == 0)
  415. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  416. /*
  417. * Commit the last in the sequence of transactions.
  418. */
  419. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  420. error = xfs_trans_commit(args.trans);
  421. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  422. return error;
  423. out:
  424. if (args.trans)
  425. xfs_trans_cancel(args.trans);
  426. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  427. return error;
  428. }
  429. /*========================================================================
  430. * External routines when attribute list is inside the inode
  431. *========================================================================*/
  432. /*
  433. * Add a name to the shortform attribute list structure
  434. * This is the external routine.
  435. */
  436. STATIC int
  437. xfs_attr_shortform_addname(xfs_da_args_t *args)
  438. {
  439. int newsize, forkoff, retval;
  440. trace_xfs_attr_sf_addname(args);
  441. retval = xfs_attr_shortform_lookup(args);
  442. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  443. return retval;
  444. } else if (retval == -EEXIST) {
  445. if (args->flags & ATTR_CREATE)
  446. return retval;
  447. retval = xfs_attr_shortform_remove(args);
  448. if (retval)
  449. return retval;
  450. /*
  451. * Since we have removed the old attr, clear ATTR_REPLACE so
  452. * that the leaf format add routine won't trip over the attr
  453. * not being around.
  454. */
  455. args->flags &= ~ATTR_REPLACE;
  456. }
  457. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  458. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  459. return -ENOSPC;
  460. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  461. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  462. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  463. if (!forkoff)
  464. return -ENOSPC;
  465. xfs_attr_shortform_add(args, forkoff);
  466. return 0;
  467. }
  468. /*========================================================================
  469. * External routines when attribute list is one block
  470. *========================================================================*/
  471. /*
  472. * Add a name to the leaf attribute list structure
  473. *
  474. * This leaf block cannot have a "remote" value, we only call this routine
  475. * if bmap_one_block() says there is only one block (ie: no remote blks).
  476. */
  477. STATIC int
  478. xfs_attr_leaf_addname(
  479. struct xfs_da_args *args)
  480. {
  481. struct xfs_inode *dp;
  482. struct xfs_buf *bp;
  483. int retval, error, forkoff;
  484. trace_xfs_attr_leaf_addname(args);
  485. /*
  486. * Read the (only) block in the attribute list in.
  487. */
  488. dp = args->dp;
  489. args->blkno = 0;
  490. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  491. if (error)
  492. return error;
  493. /*
  494. * Look up the given attribute in the leaf block. Figure out if
  495. * the given flags produce an error or call for an atomic rename.
  496. */
  497. retval = xfs_attr3_leaf_lookup_int(bp, args);
  498. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  499. xfs_trans_brelse(args->trans, bp);
  500. return retval;
  501. } else if (retval == -EEXIST) {
  502. if (args->flags & ATTR_CREATE) { /* pure create op */
  503. xfs_trans_brelse(args->trans, bp);
  504. return retval;
  505. }
  506. trace_xfs_attr_leaf_replace(args);
  507. /* save the attribute state for later removal*/
  508. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  509. args->blkno2 = args->blkno; /* set 2nd entry info*/
  510. args->index2 = args->index;
  511. args->rmtblkno2 = args->rmtblkno;
  512. args->rmtblkcnt2 = args->rmtblkcnt;
  513. args->rmtvaluelen2 = args->rmtvaluelen;
  514. /*
  515. * clear the remote attr state now that it is saved so that the
  516. * values reflect the state of the attribute we are about to
  517. * add, not the attribute we just found and will remove later.
  518. */
  519. args->rmtblkno = 0;
  520. args->rmtblkcnt = 0;
  521. args->rmtvaluelen = 0;
  522. }
  523. /*
  524. * Add the attribute to the leaf block, transitioning to a Btree
  525. * if required.
  526. */
  527. retval = xfs_attr3_leaf_add(bp, args);
  528. if (retval == -ENOSPC) {
  529. /*
  530. * Promote the attribute list to the Btree format, then
  531. * Commit that transaction so that the node_addname() call
  532. * can manage its own transactions.
  533. */
  534. error = xfs_attr3_leaf_to_node(args);
  535. if (error)
  536. return error;
  537. error = xfs_defer_finish(&args->trans);
  538. if (error)
  539. return error;
  540. /*
  541. * Commit the current trans (including the inode) and start
  542. * a new one.
  543. */
  544. error = xfs_trans_roll_inode(&args->trans, dp);
  545. if (error)
  546. return error;
  547. /*
  548. * Fob the whole rest of the problem off on the Btree code.
  549. */
  550. error = xfs_attr_node_addname(args);
  551. return error;
  552. }
  553. /*
  554. * Commit the transaction that added the attr name so that
  555. * later routines can manage their own transactions.
  556. */
  557. error = xfs_trans_roll_inode(&args->trans, dp);
  558. if (error)
  559. return error;
  560. /*
  561. * If there was an out-of-line value, allocate the blocks we
  562. * identified for its storage and copy the value. This is done
  563. * after we create the attribute so that we don't overflow the
  564. * maximum size of a transaction and/or hit a deadlock.
  565. */
  566. if (args->rmtblkno > 0) {
  567. error = xfs_attr_rmtval_set(args);
  568. if (error)
  569. return error;
  570. }
  571. /*
  572. * If this is an atomic rename operation, we must "flip" the
  573. * incomplete flags on the "new" and "old" attribute/value pairs
  574. * so that one disappears and one appears atomically. Then we
  575. * must remove the "old" attribute/value pair.
  576. */
  577. if (args->op_flags & XFS_DA_OP_RENAME) {
  578. /*
  579. * In a separate transaction, set the incomplete flag on the
  580. * "old" attr and clear the incomplete flag on the "new" attr.
  581. */
  582. error = xfs_attr3_leaf_flipflags(args);
  583. if (error)
  584. return error;
  585. /*
  586. * Dismantle the "old" attribute/value pair by removing
  587. * a "remote" value (if it exists).
  588. */
  589. args->index = args->index2;
  590. args->blkno = args->blkno2;
  591. args->rmtblkno = args->rmtblkno2;
  592. args->rmtblkcnt = args->rmtblkcnt2;
  593. args->rmtvaluelen = args->rmtvaluelen2;
  594. if (args->rmtblkno) {
  595. error = xfs_attr_rmtval_remove(args);
  596. if (error)
  597. return error;
  598. }
  599. /*
  600. * Read in the block containing the "old" attr, then
  601. * remove the "old" attr from that block (neat, huh!)
  602. */
  603. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  604. -1, &bp);
  605. if (error)
  606. return error;
  607. xfs_attr3_leaf_remove(bp, args);
  608. /*
  609. * If the result is small enough, shrink it all into the inode.
  610. */
  611. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  612. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  613. /* bp is gone due to xfs_da_shrink_inode */
  614. if (error)
  615. return error;
  616. error = xfs_defer_finish(&args->trans);
  617. if (error)
  618. return error;
  619. }
  620. /*
  621. * Commit the remove and start the next trans in series.
  622. */
  623. error = xfs_trans_roll_inode(&args->trans, dp);
  624. } else if (args->rmtblkno > 0) {
  625. /*
  626. * Added a "remote" value, just clear the incomplete flag.
  627. */
  628. error = xfs_attr3_leaf_clearflag(args);
  629. }
  630. return error;
  631. }
  632. /*
  633. * Remove a name from the leaf attribute list structure
  634. *
  635. * This leaf block cannot have a "remote" value, we only call this routine
  636. * if bmap_one_block() says there is only one block (ie: no remote blks).
  637. */
  638. STATIC int
  639. xfs_attr_leaf_removename(
  640. struct xfs_da_args *args)
  641. {
  642. struct xfs_inode *dp;
  643. struct xfs_buf *bp;
  644. int error, forkoff;
  645. trace_xfs_attr_leaf_removename(args);
  646. /*
  647. * Remove the attribute.
  648. */
  649. dp = args->dp;
  650. args->blkno = 0;
  651. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  652. if (error)
  653. return error;
  654. error = xfs_attr3_leaf_lookup_int(bp, args);
  655. if (error == -ENOATTR) {
  656. xfs_trans_brelse(args->trans, bp);
  657. return error;
  658. }
  659. xfs_attr3_leaf_remove(bp, args);
  660. /*
  661. * If the result is small enough, shrink it all into the inode.
  662. */
  663. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  664. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  665. /* bp is gone due to xfs_da_shrink_inode */
  666. if (error)
  667. return error;
  668. error = xfs_defer_finish(&args->trans);
  669. if (error)
  670. return error;
  671. }
  672. return 0;
  673. }
  674. /*
  675. * Look up a name in a leaf attribute list structure.
  676. *
  677. * This leaf block cannot have a "remote" value, we only call this routine
  678. * if bmap_one_block() says there is only one block (ie: no remote blks).
  679. */
  680. STATIC int
  681. xfs_attr_leaf_get(xfs_da_args_t *args)
  682. {
  683. struct xfs_buf *bp;
  684. int error;
  685. trace_xfs_attr_leaf_get(args);
  686. args->blkno = 0;
  687. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  688. if (error)
  689. return error;
  690. error = xfs_attr3_leaf_lookup_int(bp, args);
  691. if (error != -EEXIST) {
  692. xfs_trans_brelse(args->trans, bp);
  693. return error;
  694. }
  695. error = xfs_attr3_leaf_getvalue(bp, args);
  696. xfs_trans_brelse(args->trans, bp);
  697. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  698. error = xfs_attr_rmtval_get(args);
  699. }
  700. return error;
  701. }
  702. /*========================================================================
  703. * External routines when attribute list size > geo->blksize
  704. *========================================================================*/
  705. /*
  706. * Add a name to a Btree-format attribute list.
  707. *
  708. * This will involve walking down the Btree, and may involve splitting
  709. * leaf nodes and even splitting intermediate nodes up to and including
  710. * the root node (a special case of an intermediate node).
  711. *
  712. * "Remote" attribute values confuse the issue and atomic rename operations
  713. * add a whole extra layer of confusion on top of that.
  714. */
  715. STATIC int
  716. xfs_attr_node_addname(
  717. struct xfs_da_args *args)
  718. {
  719. struct xfs_da_state *state;
  720. struct xfs_da_state_blk *blk;
  721. struct xfs_inode *dp;
  722. struct xfs_mount *mp;
  723. int retval, error;
  724. trace_xfs_attr_node_addname(args);
  725. /*
  726. * Fill in bucket of arguments/results/context to carry around.
  727. */
  728. dp = args->dp;
  729. mp = dp->i_mount;
  730. restart:
  731. state = xfs_da_state_alloc();
  732. state->args = args;
  733. state->mp = mp;
  734. /*
  735. * Search to see if name already exists, and get back a pointer
  736. * to where it should go.
  737. */
  738. error = xfs_da3_node_lookup_int(state, &retval);
  739. if (error)
  740. goto out;
  741. blk = &state->path.blk[ state->path.active-1 ];
  742. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  743. if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
  744. goto out;
  745. } else if (retval == -EEXIST) {
  746. if (args->flags & ATTR_CREATE)
  747. goto out;
  748. trace_xfs_attr_node_replace(args);
  749. /* save the attribute state for later removal*/
  750. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  751. args->blkno2 = args->blkno; /* set 2nd entry info*/
  752. args->index2 = args->index;
  753. args->rmtblkno2 = args->rmtblkno;
  754. args->rmtblkcnt2 = args->rmtblkcnt;
  755. args->rmtvaluelen2 = args->rmtvaluelen;
  756. /*
  757. * clear the remote attr state now that it is saved so that the
  758. * values reflect the state of the attribute we are about to
  759. * add, not the attribute we just found and will remove later.
  760. */
  761. args->rmtblkno = 0;
  762. args->rmtblkcnt = 0;
  763. args->rmtvaluelen = 0;
  764. }
  765. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  766. if (retval == -ENOSPC) {
  767. if (state->path.active == 1) {
  768. /*
  769. * Its really a single leaf node, but it had
  770. * out-of-line values so it looked like it *might*
  771. * have been a b-tree.
  772. */
  773. xfs_da_state_free(state);
  774. state = NULL;
  775. error = xfs_attr3_leaf_to_node(args);
  776. if (error)
  777. goto out;
  778. error = xfs_defer_finish(&args->trans);
  779. if (error)
  780. goto out;
  781. /*
  782. * Commit the node conversion and start the next
  783. * trans in the chain.
  784. */
  785. error = xfs_trans_roll_inode(&args->trans, dp);
  786. if (error)
  787. goto out;
  788. goto restart;
  789. }
  790. /*
  791. * Split as many Btree elements as required.
  792. * This code tracks the new and old attr's location
  793. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  794. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  795. */
  796. error = xfs_da3_split(state);
  797. if (error)
  798. goto out;
  799. error = xfs_defer_finish(&args->trans);
  800. if (error)
  801. goto out;
  802. } else {
  803. /*
  804. * Addition succeeded, update Btree hashvals.
  805. */
  806. xfs_da3_fixhashpath(state, &state->path);
  807. }
  808. /*
  809. * Kill the state structure, we're done with it and need to
  810. * allow the buffers to come back later.
  811. */
  812. xfs_da_state_free(state);
  813. state = NULL;
  814. /*
  815. * Commit the leaf addition or btree split and start the next
  816. * trans in the chain.
  817. */
  818. error = xfs_trans_roll_inode(&args->trans, dp);
  819. if (error)
  820. goto out;
  821. /*
  822. * If there was an out-of-line value, allocate the blocks we
  823. * identified for its storage and copy the value. This is done
  824. * after we create the attribute so that we don't overflow the
  825. * maximum size of a transaction and/or hit a deadlock.
  826. */
  827. if (args->rmtblkno > 0) {
  828. error = xfs_attr_rmtval_set(args);
  829. if (error)
  830. return error;
  831. }
  832. /*
  833. * If this is an atomic rename operation, we must "flip" the
  834. * incomplete flags on the "new" and "old" attribute/value pairs
  835. * so that one disappears and one appears atomically. Then we
  836. * must remove the "old" attribute/value pair.
  837. */
  838. if (args->op_flags & XFS_DA_OP_RENAME) {
  839. /*
  840. * In a separate transaction, set the incomplete flag on the
  841. * "old" attr and clear the incomplete flag on the "new" attr.
  842. */
  843. error = xfs_attr3_leaf_flipflags(args);
  844. if (error)
  845. goto out;
  846. /*
  847. * Dismantle the "old" attribute/value pair by removing
  848. * a "remote" value (if it exists).
  849. */
  850. args->index = args->index2;
  851. args->blkno = args->blkno2;
  852. args->rmtblkno = args->rmtblkno2;
  853. args->rmtblkcnt = args->rmtblkcnt2;
  854. args->rmtvaluelen = args->rmtvaluelen2;
  855. if (args->rmtblkno) {
  856. error = xfs_attr_rmtval_remove(args);
  857. if (error)
  858. return error;
  859. }
  860. /*
  861. * Re-find the "old" attribute entry after any split ops.
  862. * The INCOMPLETE flag means that we will find the "old"
  863. * attr, not the "new" one.
  864. */
  865. args->flags |= XFS_ATTR_INCOMPLETE;
  866. state = xfs_da_state_alloc();
  867. state->args = args;
  868. state->mp = mp;
  869. state->inleaf = 0;
  870. error = xfs_da3_node_lookup_int(state, &retval);
  871. if (error)
  872. goto out;
  873. /*
  874. * Remove the name and update the hashvals in the tree.
  875. */
  876. blk = &state->path.blk[ state->path.active-1 ];
  877. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  878. error = xfs_attr3_leaf_remove(blk->bp, args);
  879. xfs_da3_fixhashpath(state, &state->path);
  880. /*
  881. * Check to see if the tree needs to be collapsed.
  882. */
  883. if (retval && (state->path.active > 1)) {
  884. error = xfs_da3_join(state);
  885. if (error)
  886. goto out;
  887. error = xfs_defer_finish(&args->trans);
  888. if (error)
  889. goto out;
  890. }
  891. /*
  892. * Commit and start the next trans in the chain.
  893. */
  894. error = xfs_trans_roll_inode(&args->trans, dp);
  895. if (error)
  896. goto out;
  897. } else if (args->rmtblkno > 0) {
  898. /*
  899. * Added a "remote" value, just clear the incomplete flag.
  900. */
  901. error = xfs_attr3_leaf_clearflag(args);
  902. if (error)
  903. goto out;
  904. }
  905. retval = error = 0;
  906. out:
  907. if (state)
  908. xfs_da_state_free(state);
  909. if (error)
  910. return error;
  911. return retval;
  912. }
  913. /*
  914. * Remove a name from a B-tree attribute list.
  915. *
  916. * This will involve walking down the Btree, and may involve joining
  917. * leaf nodes and even joining intermediate nodes up to and including
  918. * the root node (a special case of an intermediate node).
  919. */
  920. STATIC int
  921. xfs_attr_node_removename(
  922. struct xfs_da_args *args)
  923. {
  924. struct xfs_da_state *state;
  925. struct xfs_da_state_blk *blk;
  926. struct xfs_inode *dp;
  927. struct xfs_buf *bp;
  928. int retval, error, forkoff;
  929. trace_xfs_attr_node_removename(args);
  930. /*
  931. * Tie a string around our finger to remind us where we are.
  932. */
  933. dp = args->dp;
  934. state = xfs_da_state_alloc();
  935. state->args = args;
  936. state->mp = dp->i_mount;
  937. /*
  938. * Search to see if name exists, and get back a pointer to it.
  939. */
  940. error = xfs_da3_node_lookup_int(state, &retval);
  941. if (error || (retval != -EEXIST)) {
  942. if (error == 0)
  943. error = retval;
  944. goto out;
  945. }
  946. /*
  947. * If there is an out-of-line value, de-allocate the blocks.
  948. * This is done before we remove the attribute so that we don't
  949. * overflow the maximum size of a transaction and/or hit a deadlock.
  950. */
  951. blk = &state->path.blk[ state->path.active-1 ];
  952. ASSERT(blk->bp != NULL);
  953. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  954. if (args->rmtblkno > 0) {
  955. /*
  956. * Fill in disk block numbers in the state structure
  957. * so that we can get the buffers back after we commit
  958. * several transactions in the following calls.
  959. */
  960. error = xfs_attr_fillstate(state);
  961. if (error)
  962. goto out;
  963. /*
  964. * Mark the attribute as INCOMPLETE, then bunmapi() the
  965. * remote value.
  966. */
  967. error = xfs_attr3_leaf_setflag(args);
  968. if (error)
  969. goto out;
  970. error = xfs_attr_rmtval_remove(args);
  971. if (error)
  972. goto out;
  973. /*
  974. * Refill the state structure with buffers, the prior calls
  975. * released our buffers.
  976. */
  977. error = xfs_attr_refillstate(state);
  978. if (error)
  979. goto out;
  980. }
  981. /*
  982. * Remove the name and update the hashvals in the tree.
  983. */
  984. blk = &state->path.blk[ state->path.active-1 ];
  985. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  986. retval = xfs_attr3_leaf_remove(blk->bp, args);
  987. xfs_da3_fixhashpath(state, &state->path);
  988. /*
  989. * Check to see if the tree needs to be collapsed.
  990. */
  991. if (retval && (state->path.active > 1)) {
  992. error = xfs_da3_join(state);
  993. if (error)
  994. goto out;
  995. error = xfs_defer_finish(&args->trans);
  996. if (error)
  997. goto out;
  998. /*
  999. * Commit the Btree join operation and start a new trans.
  1000. */
  1001. error = xfs_trans_roll_inode(&args->trans, dp);
  1002. if (error)
  1003. goto out;
  1004. }
  1005. /*
  1006. * If the result is small enough, push it all into the inode.
  1007. */
  1008. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1009. /*
  1010. * Have to get rid of the copy of this dabuf in the state.
  1011. */
  1012. ASSERT(state->path.active == 1);
  1013. ASSERT(state->path.blk[0].bp);
  1014. state->path.blk[0].bp = NULL;
  1015. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1016. if (error)
  1017. goto out;
  1018. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1019. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1020. /* bp is gone due to xfs_da_shrink_inode */
  1021. if (error)
  1022. goto out;
  1023. error = xfs_defer_finish(&args->trans);
  1024. if (error)
  1025. goto out;
  1026. } else
  1027. xfs_trans_brelse(args->trans, bp);
  1028. }
  1029. error = 0;
  1030. out:
  1031. xfs_da_state_free(state);
  1032. return error;
  1033. }
  1034. /*
  1035. * Fill in the disk block numbers in the state structure for the buffers
  1036. * that are attached to the state structure.
  1037. * This is done so that we can quickly reattach ourselves to those buffers
  1038. * after some set of transaction commits have released these buffers.
  1039. */
  1040. STATIC int
  1041. xfs_attr_fillstate(xfs_da_state_t *state)
  1042. {
  1043. xfs_da_state_path_t *path;
  1044. xfs_da_state_blk_t *blk;
  1045. int level;
  1046. trace_xfs_attr_fillstate(state->args);
  1047. /*
  1048. * Roll down the "path" in the state structure, storing the on-disk
  1049. * block number for those buffers in the "path".
  1050. */
  1051. path = &state->path;
  1052. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1053. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1054. if (blk->bp) {
  1055. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1056. blk->bp = NULL;
  1057. } else {
  1058. blk->disk_blkno = 0;
  1059. }
  1060. }
  1061. /*
  1062. * Roll down the "altpath" in the state structure, storing the on-disk
  1063. * block number for those buffers in the "altpath".
  1064. */
  1065. path = &state->altpath;
  1066. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1067. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1068. if (blk->bp) {
  1069. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1070. blk->bp = NULL;
  1071. } else {
  1072. blk->disk_blkno = 0;
  1073. }
  1074. }
  1075. return 0;
  1076. }
  1077. /*
  1078. * Reattach the buffers to the state structure based on the disk block
  1079. * numbers stored in the state structure.
  1080. * This is done after some set of transaction commits have released those
  1081. * buffers from our grip.
  1082. */
  1083. STATIC int
  1084. xfs_attr_refillstate(xfs_da_state_t *state)
  1085. {
  1086. xfs_da_state_path_t *path;
  1087. xfs_da_state_blk_t *blk;
  1088. int level, error;
  1089. trace_xfs_attr_refillstate(state->args);
  1090. /*
  1091. * Roll down the "path" in the state structure, storing the on-disk
  1092. * block number for those buffers in the "path".
  1093. */
  1094. path = &state->path;
  1095. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1096. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1097. if (blk->disk_blkno) {
  1098. error = xfs_da3_node_read(state->args->trans,
  1099. state->args->dp,
  1100. blk->blkno, blk->disk_blkno,
  1101. &blk->bp, XFS_ATTR_FORK);
  1102. if (error)
  1103. return error;
  1104. } else {
  1105. blk->bp = NULL;
  1106. }
  1107. }
  1108. /*
  1109. * Roll down the "altpath" in the state structure, storing the on-disk
  1110. * block number for those buffers in the "altpath".
  1111. */
  1112. path = &state->altpath;
  1113. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1114. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1115. if (blk->disk_blkno) {
  1116. error = xfs_da3_node_read(state->args->trans,
  1117. state->args->dp,
  1118. blk->blkno, blk->disk_blkno,
  1119. &blk->bp, XFS_ATTR_FORK);
  1120. if (error)
  1121. return error;
  1122. } else {
  1123. blk->bp = NULL;
  1124. }
  1125. }
  1126. return 0;
  1127. }
  1128. /*
  1129. * Look up a filename in a node attribute list.
  1130. *
  1131. * This routine gets called for any attribute fork that has more than one
  1132. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1133. * "remote" values taking up more blocks.
  1134. */
  1135. STATIC int
  1136. xfs_attr_node_get(xfs_da_args_t *args)
  1137. {
  1138. xfs_da_state_t *state;
  1139. xfs_da_state_blk_t *blk;
  1140. int error, retval;
  1141. int i;
  1142. trace_xfs_attr_node_get(args);
  1143. state = xfs_da_state_alloc();
  1144. state->args = args;
  1145. state->mp = args->dp->i_mount;
  1146. /*
  1147. * Search to see if name exists, and get back a pointer to it.
  1148. */
  1149. error = xfs_da3_node_lookup_int(state, &retval);
  1150. if (error) {
  1151. retval = error;
  1152. } else if (retval == -EEXIST) {
  1153. blk = &state->path.blk[ state->path.active-1 ];
  1154. ASSERT(blk->bp != NULL);
  1155. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1156. /*
  1157. * Get the value, local or "remote"
  1158. */
  1159. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1160. if (!retval && (args->rmtblkno > 0)
  1161. && !(args->flags & ATTR_KERNOVAL)) {
  1162. retval = xfs_attr_rmtval_get(args);
  1163. }
  1164. }
  1165. /*
  1166. * If not in a transaction, we have to release all the buffers.
  1167. */
  1168. for (i = 0; i < state->path.active; i++) {
  1169. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1170. state->path.blk[i].bp = NULL;
  1171. }
  1172. xfs_da_state_free(state);
  1173. return retval;
  1174. }