xfs_attr.c 38 KB

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