xfs_attr.c 39 KB

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