xfs_attr.c 36 KB

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