xfs_attr.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  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_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_attr_sf.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_attr.h"
  37. #include "xfs_attr_leaf.h"
  38. #include "xfs_error.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_trans_space.h"
  41. #include "xfs_rw.h"
  42. #include "xfs_vnodeops.h"
  43. #include "xfs_trace.h"
  44. /*
  45. * xfs_attr.c
  46. *
  47. * Provide the external interfaces to manage attribute lists.
  48. */
  49. /*========================================================================
  50. * Function prototypes for the kernel.
  51. *========================================================================*/
  52. /*
  53. * Internal routines when attribute list fits inside the inode.
  54. */
  55. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  56. /*
  57. * Internal routines when attribute list is one block.
  58. */
  59. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  60. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  61. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  62. STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
  63. /*
  64. * Internal routines when attribute list is more than one block.
  65. */
  66. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  67. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  68. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  69. STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
  70. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  71. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  72. /*
  73. * Routines to manipulate out-of-line attribute values.
  74. */
  75. STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
  76. STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
  77. #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
  78. STATIC int
  79. xfs_attr_name_to_xname(
  80. struct xfs_name *xname,
  81. const unsigned char *aname)
  82. {
  83. if (!aname)
  84. return EINVAL;
  85. xname->name = aname;
  86. xname->len = strlen((char *)aname);
  87. if (xname->len >= MAXNAMELEN)
  88. return EFAULT; /* match IRIX behaviour */
  89. return 0;
  90. }
  91. STATIC int
  92. xfs_inode_hasattr(
  93. struct xfs_inode *ip)
  94. {
  95. if (!XFS_IFORK_Q(ip) ||
  96. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  97. ip->i_d.di_anextents == 0))
  98. return 0;
  99. return 1;
  100. }
  101. /*========================================================================
  102. * Overall external interface routines.
  103. *========================================================================*/
  104. STATIC int
  105. xfs_attr_get_int(
  106. struct xfs_inode *ip,
  107. struct xfs_name *name,
  108. unsigned char *value,
  109. int *valuelenp,
  110. int flags)
  111. {
  112. xfs_da_args_t args;
  113. int error;
  114. if (!xfs_inode_hasattr(ip))
  115. return ENOATTR;
  116. /*
  117. * Fill in the arg structure for this request.
  118. */
  119. memset((char *)&args, 0, sizeof(args));
  120. args.name = name->name;
  121. args.namelen = name->len;
  122. args.value = value;
  123. args.valuelen = *valuelenp;
  124. args.flags = flags;
  125. args.hashval = xfs_da_hashname(args.name, args.namelen);
  126. args.dp = ip;
  127. args.whichfork = XFS_ATTR_FORK;
  128. /*
  129. * Decide on what work routines to call based on the inode size.
  130. */
  131. if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  132. error = xfs_attr_shortform_getvalue(&args);
  133. } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
  134. error = xfs_attr_leaf_get(&args);
  135. } else {
  136. error = xfs_attr_node_get(&args);
  137. }
  138. /*
  139. * Return the number of bytes in the value to the caller.
  140. */
  141. *valuelenp = args.valuelen;
  142. if (error == EEXIST)
  143. error = 0;
  144. return(error);
  145. }
  146. int
  147. xfs_attr_get(
  148. xfs_inode_t *ip,
  149. const unsigned char *name,
  150. unsigned char *value,
  151. int *valuelenp,
  152. int flags)
  153. {
  154. int error;
  155. struct xfs_name xname;
  156. XFS_STATS_INC(xs_attr_get);
  157. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  158. return(EIO);
  159. error = xfs_attr_name_to_xname(&xname, name);
  160. if (error)
  161. return error;
  162. xfs_ilock(ip, XFS_ILOCK_SHARED);
  163. error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
  164. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  165. return(error);
  166. }
  167. /*
  168. * Calculate how many blocks we need for the new attribute,
  169. */
  170. STATIC int
  171. xfs_attr_calc_size(
  172. struct xfs_inode *ip,
  173. int namelen,
  174. int valuelen,
  175. int *local)
  176. {
  177. struct xfs_mount *mp = ip->i_mount;
  178. int size;
  179. int nblks;
  180. /*
  181. * Determine space new attribute will use, and if it would be
  182. * "local" or "remote" (note: local != inline).
  183. */
  184. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  185. mp->m_sb.sb_blocksize, local);
  186. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  187. if (*local) {
  188. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  189. /* Double split possible */
  190. nblks *= 2;
  191. }
  192. } else {
  193. /*
  194. * Out of line attribute, cannot double split, but
  195. * make room for the attribute value itself.
  196. */
  197. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  198. nblks += dblocks;
  199. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  200. }
  201. return nblks;
  202. }
  203. STATIC int
  204. xfs_attr_set_int(
  205. struct xfs_inode *dp,
  206. struct xfs_name *name,
  207. unsigned char *value,
  208. int valuelen,
  209. int flags)
  210. {
  211. xfs_da_args_t args;
  212. xfs_fsblock_t firstblock;
  213. xfs_bmap_free_t flist;
  214. int error, err2, committed;
  215. xfs_mount_t *mp = dp->i_mount;
  216. int rsvd = (flags & ATTR_ROOT) != 0;
  217. int local;
  218. /*
  219. * Attach the dquots to the inode.
  220. */
  221. error = xfs_qm_dqattach(dp, 0);
  222. if (error)
  223. return error;
  224. /*
  225. * If the inode doesn't have an attribute fork, add one.
  226. * (inode must not be locked when we call this routine)
  227. */
  228. if (XFS_IFORK_Q(dp) == 0) {
  229. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  230. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  231. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  232. return(error);
  233. }
  234. /*
  235. * Fill in the arg structure for this request.
  236. */
  237. memset((char *)&args, 0, sizeof(args));
  238. args.name = name->name;
  239. args.namelen = name->len;
  240. args.value = value;
  241. args.valuelen = valuelen;
  242. args.flags = flags;
  243. args.hashval = xfs_da_hashname(args.name, args.namelen);
  244. args.dp = dp;
  245. args.firstblock = &firstblock;
  246. args.flist = &flist;
  247. args.whichfork = XFS_ATTR_FORK;
  248. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  249. /* Size is now blocks for attribute data */
  250. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  251. /*
  252. * Start our first transaction of the day.
  253. *
  254. * All future transactions during this code must be "chained" off
  255. * this one via the trans_dup() call. All transactions will contain
  256. * the inode, and the inode will always be marked with trans_ihold().
  257. * Since the inode will be locked in all transactions, we must log
  258. * the inode in every transaction to let it float upward through
  259. * the log.
  260. */
  261. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  262. /*
  263. * Root fork attributes can use reserved data blocks for this
  264. * operation if necessary
  265. */
  266. if (rsvd)
  267. args.trans->t_flags |= XFS_TRANS_RESERVE;
  268. if ((error = xfs_trans_reserve(args.trans, args.total,
  269. XFS_ATTRSET_LOG_RES(mp, args.total), 0,
  270. XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
  271. xfs_trans_cancel(args.trans, 0);
  272. return(error);
  273. }
  274. xfs_ilock(dp, XFS_ILOCK_EXCL);
  275. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  276. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  277. XFS_QMOPT_RES_REGBLKS);
  278. if (error) {
  279. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  280. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  281. return (error);
  282. }
  283. xfs_trans_ijoin(args.trans, dp);
  284. /*
  285. * If the attribute list is non-existent or a shortform list,
  286. * upgrade it to a single-leaf-block attribute list.
  287. */
  288. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  289. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  290. (dp->i_d.di_anextents == 0))) {
  291. /*
  292. * Build initial attribute list (if required).
  293. */
  294. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  295. xfs_attr_shortform_create(&args);
  296. /*
  297. * Try to add the attr to the attribute list in
  298. * the inode.
  299. */
  300. error = xfs_attr_shortform_addname(&args);
  301. if (error != ENOSPC) {
  302. /*
  303. * Commit the shortform mods, and we're done.
  304. * NOTE: this is also the error path (EEXIST, etc).
  305. */
  306. ASSERT(args.trans != NULL);
  307. /*
  308. * If this is a synchronous mount, make sure that
  309. * the transaction goes to disk before returning
  310. * to the user.
  311. */
  312. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  313. xfs_trans_set_sync(args.trans);
  314. }
  315. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  316. xfs_trans_ichgtime(args.trans, dp,
  317. XFS_ICHGTIME_CHG);
  318. }
  319. err2 = xfs_trans_commit(args.trans,
  320. XFS_TRANS_RELEASE_LOG_RES);
  321. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  322. return(error == 0 ? err2 : error);
  323. }
  324. /*
  325. * It won't fit in the shortform, transform to a leaf block.
  326. * GROT: another possible req'mt for a double-split btree op.
  327. */
  328. xfs_bmap_init(args.flist, args.firstblock);
  329. error = xfs_attr_shortform_to_leaf(&args);
  330. if (!error) {
  331. error = xfs_bmap_finish(&args.trans, args.flist,
  332. &committed);
  333. }
  334. if (error) {
  335. ASSERT(committed);
  336. args.trans = NULL;
  337. xfs_bmap_cancel(&flist);
  338. goto out;
  339. }
  340. /*
  341. * bmap_finish() may have committed the last trans and started
  342. * a new one. We need the inode to be in all transactions.
  343. */
  344. if (committed)
  345. xfs_trans_ijoin(args.trans, dp);
  346. /*
  347. * Commit the leaf transformation. We'll need another (linked)
  348. * transaction to add the new attribute to the leaf.
  349. */
  350. error = xfs_trans_roll(&args.trans, dp);
  351. if (error)
  352. goto out;
  353. }
  354. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  355. error = xfs_attr_leaf_addname(&args);
  356. } else {
  357. error = xfs_attr_node_addname(&args);
  358. }
  359. if (error) {
  360. goto out;
  361. }
  362. /*
  363. * If this is a synchronous mount, make sure that the
  364. * transaction goes to disk before returning to the user.
  365. */
  366. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  367. xfs_trans_set_sync(args.trans);
  368. }
  369. if ((flags & ATTR_KERNOTIME) == 0)
  370. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  371. /*
  372. * Commit the last in the sequence of transactions.
  373. */
  374. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  375. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  376. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  377. return(error);
  378. out:
  379. if (args.trans)
  380. xfs_trans_cancel(args.trans,
  381. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  382. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  383. return(error);
  384. }
  385. int
  386. xfs_attr_set(
  387. xfs_inode_t *dp,
  388. const unsigned char *name,
  389. unsigned char *value,
  390. int valuelen,
  391. int flags)
  392. {
  393. int error;
  394. struct xfs_name xname;
  395. XFS_STATS_INC(xs_attr_set);
  396. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  397. return (EIO);
  398. error = xfs_attr_name_to_xname(&xname, name);
  399. if (error)
  400. return error;
  401. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  402. }
  403. /*
  404. * Generic handler routine to remove a name from an attribute list.
  405. * Transitions attribute list from Btree to shortform as necessary.
  406. */
  407. STATIC int
  408. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  409. {
  410. xfs_da_args_t args;
  411. xfs_fsblock_t firstblock;
  412. xfs_bmap_free_t flist;
  413. int error;
  414. xfs_mount_t *mp = dp->i_mount;
  415. /*
  416. * Fill in the arg structure for this request.
  417. */
  418. memset((char *)&args, 0, sizeof(args));
  419. args.name = name->name;
  420. args.namelen = name->len;
  421. args.flags = flags;
  422. args.hashval = xfs_da_hashname(args.name, args.namelen);
  423. args.dp = dp;
  424. args.firstblock = &firstblock;
  425. args.flist = &flist;
  426. args.total = 0;
  427. args.whichfork = XFS_ATTR_FORK;
  428. /*
  429. * Attach the dquots to the inode.
  430. */
  431. error = xfs_qm_dqattach(dp, 0);
  432. if (error)
  433. return error;
  434. /*
  435. * Start our first transaction of the day.
  436. *
  437. * All future transactions during this code must be "chained" off
  438. * this one via the trans_dup() call. All transactions will contain
  439. * the inode, and the inode will always be marked with trans_ihold().
  440. * Since the inode will be locked in all transactions, we must log
  441. * the inode in every transaction to let it float upward through
  442. * the log.
  443. */
  444. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  445. /*
  446. * Root fork attributes can use reserved data blocks for this
  447. * operation if necessary
  448. */
  449. if (flags & ATTR_ROOT)
  450. args.trans->t_flags |= XFS_TRANS_RESERVE;
  451. if ((error = xfs_trans_reserve(args.trans,
  452. XFS_ATTRRM_SPACE_RES(mp),
  453. XFS_ATTRRM_LOG_RES(mp),
  454. 0, XFS_TRANS_PERM_LOG_RES,
  455. XFS_ATTRRM_LOG_COUNT))) {
  456. xfs_trans_cancel(args.trans, 0);
  457. return(error);
  458. }
  459. xfs_ilock(dp, XFS_ILOCK_EXCL);
  460. /*
  461. * No need to make quota reservations here. We expect to release some
  462. * blocks not allocate in the common case.
  463. */
  464. xfs_trans_ijoin(args.trans, dp);
  465. /*
  466. * Decide on what work routines to call based on the inode size.
  467. */
  468. if (!xfs_inode_hasattr(dp)) {
  469. error = XFS_ERROR(ENOATTR);
  470. goto out;
  471. }
  472. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  473. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  474. error = xfs_attr_shortform_remove(&args);
  475. if (error) {
  476. goto out;
  477. }
  478. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  479. error = xfs_attr_leaf_removename(&args);
  480. } else {
  481. error = xfs_attr_node_removename(&args);
  482. }
  483. if (error) {
  484. goto out;
  485. }
  486. /*
  487. * If this is a synchronous mount, make sure that the
  488. * transaction goes to disk before returning to the user.
  489. */
  490. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  491. xfs_trans_set_sync(args.trans);
  492. }
  493. if ((flags & ATTR_KERNOTIME) == 0)
  494. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  495. /*
  496. * Commit the last in the sequence of transactions.
  497. */
  498. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  499. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  500. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  501. return(error);
  502. out:
  503. if (args.trans)
  504. xfs_trans_cancel(args.trans,
  505. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  506. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  507. return(error);
  508. }
  509. int
  510. xfs_attr_remove(
  511. xfs_inode_t *dp,
  512. const unsigned char *name,
  513. int flags)
  514. {
  515. int error;
  516. struct xfs_name xname;
  517. XFS_STATS_INC(xs_attr_remove);
  518. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  519. return (EIO);
  520. error = xfs_attr_name_to_xname(&xname, name);
  521. if (error)
  522. return error;
  523. xfs_ilock(dp, XFS_ILOCK_SHARED);
  524. if (!xfs_inode_hasattr(dp)) {
  525. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  526. return XFS_ERROR(ENOATTR);
  527. }
  528. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  529. return xfs_attr_remove_int(dp, &xname, flags);
  530. }
  531. int
  532. xfs_attr_list_int(xfs_attr_list_context_t *context)
  533. {
  534. int error;
  535. xfs_inode_t *dp = context->dp;
  536. XFS_STATS_INC(xs_attr_list);
  537. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  538. return EIO;
  539. xfs_ilock(dp, XFS_ILOCK_SHARED);
  540. /*
  541. * Decide on what work routines to call based on the inode size.
  542. */
  543. if (!xfs_inode_hasattr(dp)) {
  544. error = 0;
  545. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  546. error = xfs_attr_shortform_list(context);
  547. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  548. error = xfs_attr_leaf_list(context);
  549. } else {
  550. error = xfs_attr_node_list(context);
  551. }
  552. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  553. return error;
  554. }
  555. #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
  556. (((struct attrlist_ent *) 0)->a_name - (char *) 0)
  557. #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
  558. ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
  559. & ~(sizeof(u_int32_t)-1))
  560. /*
  561. * Format an attribute and copy it out to the user's buffer.
  562. * Take care to check values and protect against them changing later,
  563. * we may be reading them directly out of a user buffer.
  564. */
  565. /*ARGSUSED*/
  566. STATIC int
  567. xfs_attr_put_listent(
  568. xfs_attr_list_context_t *context,
  569. int flags,
  570. unsigned char *name,
  571. int namelen,
  572. int valuelen,
  573. unsigned char *value)
  574. {
  575. struct attrlist *alist = (struct attrlist *)context->alist;
  576. attrlist_ent_t *aep;
  577. int arraytop;
  578. ASSERT(!(context->flags & ATTR_KERNOVAL));
  579. ASSERT(context->count >= 0);
  580. ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  581. ASSERT(context->firstu >= sizeof(*alist));
  582. ASSERT(context->firstu <= context->bufsize);
  583. /*
  584. * Only list entries in the right namespace.
  585. */
  586. if (((context->flags & ATTR_SECURE) == 0) !=
  587. ((flags & XFS_ATTR_SECURE) == 0))
  588. return 0;
  589. if (((context->flags & ATTR_ROOT) == 0) !=
  590. ((flags & XFS_ATTR_ROOT) == 0))
  591. return 0;
  592. arraytop = sizeof(*alist) +
  593. context->count * sizeof(alist->al_offset[0]);
  594. context->firstu -= ATTR_ENTSIZE(namelen);
  595. if (context->firstu < arraytop) {
  596. trace_xfs_attr_list_full(context);
  597. alist->al_more = 1;
  598. context->seen_enough = 1;
  599. return 1;
  600. }
  601. aep = (attrlist_ent_t *)&context->alist[context->firstu];
  602. aep->a_valuelen = valuelen;
  603. memcpy(aep->a_name, name, namelen);
  604. aep->a_name[namelen] = 0;
  605. alist->al_offset[context->count++] = context->firstu;
  606. alist->al_count = context->count;
  607. trace_xfs_attr_list_add(context);
  608. return 0;
  609. }
  610. /*
  611. * Generate a list of extended attribute names and optionally
  612. * also value lengths. Positive return value follows the XFS
  613. * convention of being an error, zero or negative return code
  614. * is the length of the buffer returned (negated), indicating
  615. * success.
  616. */
  617. int
  618. xfs_attr_list(
  619. xfs_inode_t *dp,
  620. char *buffer,
  621. int bufsize,
  622. int flags,
  623. attrlist_cursor_kern_t *cursor)
  624. {
  625. xfs_attr_list_context_t context;
  626. struct attrlist *alist;
  627. int error;
  628. /*
  629. * Validate the cursor.
  630. */
  631. if (cursor->pad1 || cursor->pad2)
  632. return(XFS_ERROR(EINVAL));
  633. if ((cursor->initted == 0) &&
  634. (cursor->hashval || cursor->blkno || cursor->offset))
  635. return XFS_ERROR(EINVAL);
  636. /*
  637. * Check for a properly aligned buffer.
  638. */
  639. if (((long)buffer) & (sizeof(int)-1))
  640. return XFS_ERROR(EFAULT);
  641. if (flags & ATTR_KERNOVAL)
  642. bufsize = 0;
  643. /*
  644. * Initialize the output buffer.
  645. */
  646. memset(&context, 0, sizeof(context));
  647. context.dp = dp;
  648. context.cursor = cursor;
  649. context.resynch = 1;
  650. context.flags = flags;
  651. context.alist = buffer;
  652. context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
  653. context.firstu = context.bufsize;
  654. context.put_listent = xfs_attr_put_listent;
  655. alist = (struct attrlist *)context.alist;
  656. alist->al_count = 0;
  657. alist->al_more = 0;
  658. alist->al_offset[0] = context.bufsize;
  659. error = xfs_attr_list_int(&context);
  660. ASSERT(error >= 0);
  661. return error;
  662. }
  663. int /* error */
  664. xfs_attr_inactive(xfs_inode_t *dp)
  665. {
  666. xfs_trans_t *trans;
  667. xfs_mount_t *mp;
  668. int error;
  669. mp = dp->i_mount;
  670. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  671. xfs_ilock(dp, XFS_ILOCK_SHARED);
  672. if (!xfs_inode_hasattr(dp) ||
  673. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  674. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  675. return 0;
  676. }
  677. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  678. /*
  679. * Start our first transaction of the day.
  680. *
  681. * All future transactions during this code must be "chained" off
  682. * this one via the trans_dup() call. All transactions will contain
  683. * the inode, and the inode will always be marked with trans_ihold().
  684. * Since the inode will be locked in all transactions, we must log
  685. * the inode in every transaction to let it float upward through
  686. * the log.
  687. */
  688. trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
  689. if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
  690. XFS_TRANS_PERM_LOG_RES,
  691. XFS_ATTRINVAL_LOG_COUNT))) {
  692. xfs_trans_cancel(trans, 0);
  693. return(error);
  694. }
  695. xfs_ilock(dp, XFS_ILOCK_EXCL);
  696. /*
  697. * No need to make quota reservations here. We expect to release some
  698. * blocks, not allocate, in the common case.
  699. */
  700. xfs_trans_ijoin(trans, dp);
  701. /*
  702. * Decide on what work routines to call based on the inode size.
  703. */
  704. if (!xfs_inode_hasattr(dp) ||
  705. dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  706. error = 0;
  707. goto out;
  708. }
  709. error = xfs_attr_root_inactive(&trans, dp);
  710. if (error)
  711. goto out;
  712. /*
  713. * signal synchronous inactive transactions unless this
  714. * is a synchronous mount filesystem in which case we
  715. * know that we're here because we've been called out of
  716. * xfs_inactive which means that the last reference is gone
  717. * and the unlink transaction has already hit the disk so
  718. * async inactive transactions are safe.
  719. */
  720. if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
  721. (!(mp->m_flags & XFS_MOUNT_WSYNC)
  722. ? 1 : 0))))
  723. goto out;
  724. /*
  725. * Commit the last in the sequence of transactions.
  726. */
  727. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  728. error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
  729. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  730. return(error);
  731. out:
  732. xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  733. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  734. return(error);
  735. }
  736. /*========================================================================
  737. * External routines when attribute list is inside the inode
  738. *========================================================================*/
  739. /*
  740. * Add a name to the shortform attribute list structure
  741. * This is the external routine.
  742. */
  743. STATIC int
  744. xfs_attr_shortform_addname(xfs_da_args_t *args)
  745. {
  746. int newsize, forkoff, retval;
  747. retval = xfs_attr_shortform_lookup(args);
  748. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  749. return(retval);
  750. } else if (retval == EEXIST) {
  751. if (args->flags & ATTR_CREATE)
  752. return(retval);
  753. retval = xfs_attr_shortform_remove(args);
  754. ASSERT(retval == 0);
  755. }
  756. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  757. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  758. return(XFS_ERROR(ENOSPC));
  759. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  760. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  761. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  762. if (!forkoff)
  763. return(XFS_ERROR(ENOSPC));
  764. xfs_attr_shortform_add(args, forkoff);
  765. return(0);
  766. }
  767. /*========================================================================
  768. * External routines when attribute list is one block
  769. *========================================================================*/
  770. /*
  771. * Add a name to the leaf attribute list structure
  772. *
  773. * This leaf block cannot have a "remote" value, we only call this routine
  774. * if bmap_one_block() says there is only one block (ie: no remote blks).
  775. */
  776. STATIC int
  777. xfs_attr_leaf_addname(xfs_da_args_t *args)
  778. {
  779. xfs_inode_t *dp;
  780. xfs_dabuf_t *bp;
  781. int retval, error, committed, forkoff;
  782. /*
  783. * Read the (only) block in the attribute list in.
  784. */
  785. dp = args->dp;
  786. args->blkno = 0;
  787. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  788. XFS_ATTR_FORK);
  789. if (error)
  790. return(error);
  791. ASSERT(bp != NULL);
  792. /*
  793. * Look up the given attribute in the leaf block. Figure out if
  794. * the given flags produce an error or call for an atomic rename.
  795. */
  796. retval = xfs_attr_leaf_lookup_int(bp, args);
  797. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  798. xfs_da_brelse(args->trans, bp);
  799. return(retval);
  800. } else if (retval == EEXIST) {
  801. if (args->flags & ATTR_CREATE) { /* pure create op */
  802. xfs_da_brelse(args->trans, bp);
  803. return(retval);
  804. }
  805. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  806. args->blkno2 = args->blkno; /* set 2nd entry info*/
  807. args->index2 = args->index;
  808. args->rmtblkno2 = args->rmtblkno;
  809. args->rmtblkcnt2 = args->rmtblkcnt;
  810. }
  811. /*
  812. * Add the attribute to the leaf block, transitioning to a Btree
  813. * if required.
  814. */
  815. retval = xfs_attr_leaf_add(bp, args);
  816. xfs_da_buf_done(bp);
  817. if (retval == ENOSPC) {
  818. /*
  819. * Promote the attribute list to the Btree format, then
  820. * Commit that transaction so that the node_addname() call
  821. * can manage its own transactions.
  822. */
  823. xfs_bmap_init(args->flist, args->firstblock);
  824. error = xfs_attr_leaf_to_node(args);
  825. if (!error) {
  826. error = xfs_bmap_finish(&args->trans, args->flist,
  827. &committed);
  828. }
  829. if (error) {
  830. ASSERT(committed);
  831. args->trans = NULL;
  832. xfs_bmap_cancel(args->flist);
  833. return(error);
  834. }
  835. /*
  836. * bmap_finish() may have committed the last trans and started
  837. * a new one. We need the inode to be in all transactions.
  838. */
  839. if (committed)
  840. xfs_trans_ijoin(args->trans, dp);
  841. /*
  842. * Commit the current trans (including the inode) and start
  843. * a new one.
  844. */
  845. error = xfs_trans_roll(&args->trans, dp);
  846. if (error)
  847. return (error);
  848. /*
  849. * Fob the whole rest of the problem off on the Btree code.
  850. */
  851. error = xfs_attr_node_addname(args);
  852. return(error);
  853. }
  854. /*
  855. * Commit the transaction that added the attr name so that
  856. * later routines can manage their own transactions.
  857. */
  858. error = xfs_trans_roll(&args->trans, dp);
  859. if (error)
  860. return (error);
  861. /*
  862. * If there was an out-of-line value, allocate the blocks we
  863. * identified for its storage and copy the value. This is done
  864. * after we create the attribute so that we don't overflow the
  865. * maximum size of a transaction and/or hit a deadlock.
  866. */
  867. if (args->rmtblkno > 0) {
  868. error = xfs_attr_rmtval_set(args);
  869. if (error)
  870. return(error);
  871. }
  872. /*
  873. * If this is an atomic rename operation, we must "flip" the
  874. * incomplete flags on the "new" and "old" attribute/value pairs
  875. * so that one disappears and one appears atomically. Then we
  876. * must remove the "old" attribute/value pair.
  877. */
  878. if (args->op_flags & XFS_DA_OP_RENAME) {
  879. /*
  880. * In a separate transaction, set the incomplete flag on the
  881. * "old" attr and clear the incomplete flag on the "new" attr.
  882. */
  883. error = xfs_attr_leaf_flipflags(args);
  884. if (error)
  885. return(error);
  886. /*
  887. * Dismantle the "old" attribute/value pair by removing
  888. * a "remote" value (if it exists).
  889. */
  890. args->index = args->index2;
  891. args->blkno = args->blkno2;
  892. args->rmtblkno = args->rmtblkno2;
  893. args->rmtblkcnt = args->rmtblkcnt2;
  894. if (args->rmtblkno) {
  895. error = xfs_attr_rmtval_remove(args);
  896. if (error)
  897. return(error);
  898. }
  899. /*
  900. * Read in the block containing the "old" attr, then
  901. * remove the "old" attr from that block (neat, huh!)
  902. */
  903. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
  904. &bp, XFS_ATTR_FORK);
  905. if (error)
  906. return(error);
  907. ASSERT(bp != NULL);
  908. (void)xfs_attr_leaf_remove(bp, args);
  909. /*
  910. * If the result is small enough, shrink it all into the inode.
  911. */
  912. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  913. xfs_bmap_init(args->flist, args->firstblock);
  914. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  915. /* bp is gone due to xfs_da_shrink_inode */
  916. if (!error) {
  917. error = xfs_bmap_finish(&args->trans,
  918. args->flist,
  919. &committed);
  920. }
  921. if (error) {
  922. ASSERT(committed);
  923. args->trans = NULL;
  924. xfs_bmap_cancel(args->flist);
  925. return(error);
  926. }
  927. /*
  928. * bmap_finish() may have committed the last trans
  929. * and started a new one. We need the inode to be
  930. * in all transactions.
  931. */
  932. if (committed)
  933. xfs_trans_ijoin(args->trans, dp);
  934. } else
  935. xfs_da_buf_done(bp);
  936. /*
  937. * Commit the remove and start the next trans in series.
  938. */
  939. error = xfs_trans_roll(&args->trans, dp);
  940. } else if (args->rmtblkno > 0) {
  941. /*
  942. * Added a "remote" value, just clear the incomplete flag.
  943. */
  944. error = xfs_attr_leaf_clearflag(args);
  945. }
  946. return(error);
  947. }
  948. /*
  949. * Remove a name from the leaf attribute list structure
  950. *
  951. * This leaf block cannot have a "remote" value, we only call this routine
  952. * if bmap_one_block() says there is only one block (ie: no remote blks).
  953. */
  954. STATIC int
  955. xfs_attr_leaf_removename(xfs_da_args_t *args)
  956. {
  957. xfs_inode_t *dp;
  958. xfs_dabuf_t *bp;
  959. int error, committed, forkoff;
  960. /*
  961. * Remove the attribute.
  962. */
  963. dp = args->dp;
  964. args->blkno = 0;
  965. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  966. XFS_ATTR_FORK);
  967. if (error) {
  968. return(error);
  969. }
  970. ASSERT(bp != NULL);
  971. error = xfs_attr_leaf_lookup_int(bp, args);
  972. if (error == ENOATTR) {
  973. xfs_da_brelse(args->trans, bp);
  974. return(error);
  975. }
  976. (void)xfs_attr_leaf_remove(bp, args);
  977. /*
  978. * If the result is small enough, shrink it all into the inode.
  979. */
  980. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  981. xfs_bmap_init(args->flist, args->firstblock);
  982. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  983. /* bp is gone due to xfs_da_shrink_inode */
  984. if (!error) {
  985. error = xfs_bmap_finish(&args->trans, args->flist,
  986. &committed);
  987. }
  988. if (error) {
  989. ASSERT(committed);
  990. args->trans = NULL;
  991. xfs_bmap_cancel(args->flist);
  992. return(error);
  993. }
  994. /*
  995. * bmap_finish() may have committed the last trans and started
  996. * a new one. We need the inode to be in all transactions.
  997. */
  998. if (committed)
  999. xfs_trans_ijoin(args->trans, dp);
  1000. } else
  1001. xfs_da_buf_done(bp);
  1002. return(0);
  1003. }
  1004. /*
  1005. * Look up a name in a leaf attribute list structure.
  1006. *
  1007. * This leaf block cannot have a "remote" value, we only call this routine
  1008. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1009. */
  1010. STATIC int
  1011. xfs_attr_leaf_get(xfs_da_args_t *args)
  1012. {
  1013. xfs_dabuf_t *bp;
  1014. int error;
  1015. args->blkno = 0;
  1016. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  1017. XFS_ATTR_FORK);
  1018. if (error)
  1019. return(error);
  1020. ASSERT(bp != NULL);
  1021. error = xfs_attr_leaf_lookup_int(bp, args);
  1022. if (error != EEXIST) {
  1023. xfs_da_brelse(args->trans, bp);
  1024. return(error);
  1025. }
  1026. error = xfs_attr_leaf_getvalue(bp, args);
  1027. xfs_da_brelse(args->trans, bp);
  1028. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  1029. error = xfs_attr_rmtval_get(args);
  1030. }
  1031. return(error);
  1032. }
  1033. /*
  1034. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  1035. */
  1036. STATIC int
  1037. xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  1038. {
  1039. xfs_attr_leafblock_t *leaf;
  1040. int error;
  1041. xfs_dabuf_t *bp;
  1042. context->cursor->blkno = 0;
  1043. error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
  1044. if (error)
  1045. return XFS_ERROR(error);
  1046. ASSERT(bp != NULL);
  1047. leaf = bp->data;
  1048. if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
  1049. XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
  1050. context->dp->i_mount, leaf);
  1051. xfs_da_brelse(NULL, bp);
  1052. return XFS_ERROR(EFSCORRUPTED);
  1053. }
  1054. error = xfs_attr_leaf_list_int(bp, context);
  1055. xfs_da_brelse(NULL, bp);
  1056. return XFS_ERROR(error);
  1057. }
  1058. /*========================================================================
  1059. * External routines when attribute list size > XFS_LBSIZE(mp).
  1060. *========================================================================*/
  1061. /*
  1062. * Add a name to a Btree-format attribute list.
  1063. *
  1064. * This will involve walking down the Btree, and may involve splitting
  1065. * leaf nodes and even splitting intermediate nodes up to and including
  1066. * the root node (a special case of an intermediate node).
  1067. *
  1068. * "Remote" attribute values confuse the issue and atomic rename operations
  1069. * add a whole extra layer of confusion on top of that.
  1070. */
  1071. STATIC int
  1072. xfs_attr_node_addname(xfs_da_args_t *args)
  1073. {
  1074. xfs_da_state_t *state;
  1075. xfs_da_state_blk_t *blk;
  1076. xfs_inode_t *dp;
  1077. xfs_mount_t *mp;
  1078. int committed, retval, error;
  1079. /*
  1080. * Fill in bucket of arguments/results/context to carry around.
  1081. */
  1082. dp = args->dp;
  1083. mp = dp->i_mount;
  1084. restart:
  1085. state = xfs_da_state_alloc();
  1086. state->args = args;
  1087. state->mp = mp;
  1088. state->blocksize = state->mp->m_sb.sb_blocksize;
  1089. state->node_ents = state->mp->m_attr_node_ents;
  1090. /*
  1091. * Search to see if name already exists, and get back a pointer
  1092. * to where it should go.
  1093. */
  1094. error = xfs_da_node_lookup_int(state, &retval);
  1095. if (error)
  1096. goto out;
  1097. blk = &state->path.blk[ state->path.active-1 ];
  1098. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1099. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  1100. goto out;
  1101. } else if (retval == EEXIST) {
  1102. if (args->flags & ATTR_CREATE)
  1103. goto out;
  1104. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  1105. args->blkno2 = args->blkno; /* set 2nd entry info*/
  1106. args->index2 = args->index;
  1107. args->rmtblkno2 = args->rmtblkno;
  1108. args->rmtblkcnt2 = args->rmtblkcnt;
  1109. args->rmtblkno = 0;
  1110. args->rmtblkcnt = 0;
  1111. }
  1112. retval = xfs_attr_leaf_add(blk->bp, state->args);
  1113. if (retval == ENOSPC) {
  1114. if (state->path.active == 1) {
  1115. /*
  1116. * Its really a single leaf node, but it had
  1117. * out-of-line values so it looked like it *might*
  1118. * have been a b-tree.
  1119. */
  1120. xfs_da_state_free(state);
  1121. xfs_bmap_init(args->flist, args->firstblock);
  1122. error = xfs_attr_leaf_to_node(args);
  1123. if (!error) {
  1124. error = xfs_bmap_finish(&args->trans,
  1125. args->flist,
  1126. &committed);
  1127. }
  1128. if (error) {
  1129. ASSERT(committed);
  1130. args->trans = NULL;
  1131. xfs_bmap_cancel(args->flist);
  1132. goto out;
  1133. }
  1134. /*
  1135. * bmap_finish() may have committed the last trans
  1136. * and started a new one. We need the inode to be
  1137. * in all transactions.
  1138. */
  1139. if (committed)
  1140. xfs_trans_ijoin(args->trans, dp);
  1141. /*
  1142. * Commit the node conversion and start the next
  1143. * trans in the chain.
  1144. */
  1145. error = xfs_trans_roll(&args->trans, dp);
  1146. if (error)
  1147. goto out;
  1148. goto restart;
  1149. }
  1150. /*
  1151. * Split as many Btree elements as required.
  1152. * This code tracks the new and old attr's location
  1153. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1154. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1155. */
  1156. xfs_bmap_init(args->flist, args->firstblock);
  1157. error = xfs_da_split(state);
  1158. if (!error) {
  1159. error = xfs_bmap_finish(&args->trans, args->flist,
  1160. &committed);
  1161. }
  1162. if (error) {
  1163. ASSERT(committed);
  1164. args->trans = NULL;
  1165. xfs_bmap_cancel(args->flist);
  1166. goto out;
  1167. }
  1168. /*
  1169. * bmap_finish() may have committed the last trans and started
  1170. * a new one. We need the inode to be in all transactions.
  1171. */
  1172. if (committed)
  1173. xfs_trans_ijoin(args->trans, dp);
  1174. } else {
  1175. /*
  1176. * Addition succeeded, update Btree hashvals.
  1177. */
  1178. xfs_da_fixhashpath(state, &state->path);
  1179. }
  1180. /*
  1181. * Kill the state structure, we're done with it and need to
  1182. * allow the buffers to come back later.
  1183. */
  1184. xfs_da_state_free(state);
  1185. state = NULL;
  1186. /*
  1187. * Commit the leaf addition or btree split and start the next
  1188. * trans in the chain.
  1189. */
  1190. error = xfs_trans_roll(&args->trans, dp);
  1191. if (error)
  1192. goto out;
  1193. /*
  1194. * If there was an out-of-line value, allocate the blocks we
  1195. * identified for its storage and copy the value. This is done
  1196. * after we create the attribute so that we don't overflow the
  1197. * maximum size of a transaction and/or hit a deadlock.
  1198. */
  1199. if (args->rmtblkno > 0) {
  1200. error = xfs_attr_rmtval_set(args);
  1201. if (error)
  1202. return(error);
  1203. }
  1204. /*
  1205. * If this is an atomic rename operation, we must "flip" the
  1206. * incomplete flags on the "new" and "old" attribute/value pairs
  1207. * so that one disappears and one appears atomically. Then we
  1208. * must remove the "old" attribute/value pair.
  1209. */
  1210. if (args->op_flags & XFS_DA_OP_RENAME) {
  1211. /*
  1212. * In a separate transaction, set the incomplete flag on the
  1213. * "old" attr and clear the incomplete flag on the "new" attr.
  1214. */
  1215. error = xfs_attr_leaf_flipflags(args);
  1216. if (error)
  1217. goto out;
  1218. /*
  1219. * Dismantle the "old" attribute/value pair by removing
  1220. * a "remote" value (if it exists).
  1221. */
  1222. args->index = args->index2;
  1223. args->blkno = args->blkno2;
  1224. args->rmtblkno = args->rmtblkno2;
  1225. args->rmtblkcnt = args->rmtblkcnt2;
  1226. if (args->rmtblkno) {
  1227. error = xfs_attr_rmtval_remove(args);
  1228. if (error)
  1229. return(error);
  1230. }
  1231. /*
  1232. * Re-find the "old" attribute entry after any split ops.
  1233. * The INCOMPLETE flag means that we will find the "old"
  1234. * attr, not the "new" one.
  1235. */
  1236. args->flags |= XFS_ATTR_INCOMPLETE;
  1237. state = xfs_da_state_alloc();
  1238. state->args = args;
  1239. state->mp = mp;
  1240. state->blocksize = state->mp->m_sb.sb_blocksize;
  1241. state->node_ents = state->mp->m_attr_node_ents;
  1242. state->inleaf = 0;
  1243. error = xfs_da_node_lookup_int(state, &retval);
  1244. if (error)
  1245. goto out;
  1246. /*
  1247. * Remove the name and update the hashvals in the tree.
  1248. */
  1249. blk = &state->path.blk[ state->path.active-1 ];
  1250. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1251. error = xfs_attr_leaf_remove(blk->bp, args);
  1252. xfs_da_fixhashpath(state, &state->path);
  1253. /*
  1254. * Check to see if the tree needs to be collapsed.
  1255. */
  1256. if (retval && (state->path.active > 1)) {
  1257. xfs_bmap_init(args->flist, args->firstblock);
  1258. error = xfs_da_join(state);
  1259. if (!error) {
  1260. error = xfs_bmap_finish(&args->trans,
  1261. args->flist,
  1262. &committed);
  1263. }
  1264. if (error) {
  1265. ASSERT(committed);
  1266. args->trans = NULL;
  1267. xfs_bmap_cancel(args->flist);
  1268. goto out;
  1269. }
  1270. /*
  1271. * bmap_finish() may have committed the last trans
  1272. * and started a new one. We need the inode to be
  1273. * in all transactions.
  1274. */
  1275. if (committed)
  1276. xfs_trans_ijoin(args->trans, dp);
  1277. }
  1278. /*
  1279. * Commit and start the next trans in the chain.
  1280. */
  1281. error = xfs_trans_roll(&args->trans, dp);
  1282. if (error)
  1283. goto out;
  1284. } else if (args->rmtblkno > 0) {
  1285. /*
  1286. * Added a "remote" value, just clear the incomplete flag.
  1287. */
  1288. error = xfs_attr_leaf_clearflag(args);
  1289. if (error)
  1290. goto out;
  1291. }
  1292. retval = error = 0;
  1293. out:
  1294. if (state)
  1295. xfs_da_state_free(state);
  1296. if (error)
  1297. return(error);
  1298. return(retval);
  1299. }
  1300. /*
  1301. * Remove a name from a B-tree attribute list.
  1302. *
  1303. * This will involve walking down the Btree, and may involve joining
  1304. * leaf nodes and even joining intermediate nodes up to and including
  1305. * the root node (a special case of an intermediate node).
  1306. */
  1307. STATIC int
  1308. xfs_attr_node_removename(xfs_da_args_t *args)
  1309. {
  1310. xfs_da_state_t *state;
  1311. xfs_da_state_blk_t *blk;
  1312. xfs_inode_t *dp;
  1313. xfs_dabuf_t *bp;
  1314. int retval, error, committed, forkoff;
  1315. /*
  1316. * Tie a string around our finger to remind us where we are.
  1317. */
  1318. dp = args->dp;
  1319. state = xfs_da_state_alloc();
  1320. state->args = args;
  1321. state->mp = dp->i_mount;
  1322. state->blocksize = state->mp->m_sb.sb_blocksize;
  1323. state->node_ents = state->mp->m_attr_node_ents;
  1324. /*
  1325. * Search to see if name exists, and get back a pointer to it.
  1326. */
  1327. error = xfs_da_node_lookup_int(state, &retval);
  1328. if (error || (retval != EEXIST)) {
  1329. if (error == 0)
  1330. error = retval;
  1331. goto out;
  1332. }
  1333. /*
  1334. * If there is an out-of-line value, de-allocate the blocks.
  1335. * This is done before we remove the attribute so that we don't
  1336. * overflow the maximum size of a transaction and/or hit a deadlock.
  1337. */
  1338. blk = &state->path.blk[ state->path.active-1 ];
  1339. ASSERT(blk->bp != NULL);
  1340. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1341. if (args->rmtblkno > 0) {
  1342. /*
  1343. * Fill in disk block numbers in the state structure
  1344. * so that we can get the buffers back after we commit
  1345. * several transactions in the following calls.
  1346. */
  1347. error = xfs_attr_fillstate(state);
  1348. if (error)
  1349. goto out;
  1350. /*
  1351. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1352. * remote value.
  1353. */
  1354. error = xfs_attr_leaf_setflag(args);
  1355. if (error)
  1356. goto out;
  1357. error = xfs_attr_rmtval_remove(args);
  1358. if (error)
  1359. goto out;
  1360. /*
  1361. * Refill the state structure with buffers, the prior calls
  1362. * released our buffers.
  1363. */
  1364. error = xfs_attr_refillstate(state);
  1365. if (error)
  1366. goto out;
  1367. }
  1368. /*
  1369. * Remove the name and update the hashvals in the tree.
  1370. */
  1371. blk = &state->path.blk[ state->path.active-1 ];
  1372. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1373. retval = xfs_attr_leaf_remove(blk->bp, args);
  1374. xfs_da_fixhashpath(state, &state->path);
  1375. /*
  1376. * Check to see if the tree needs to be collapsed.
  1377. */
  1378. if (retval && (state->path.active > 1)) {
  1379. xfs_bmap_init(args->flist, args->firstblock);
  1380. error = xfs_da_join(state);
  1381. if (!error) {
  1382. error = xfs_bmap_finish(&args->trans, args->flist,
  1383. &committed);
  1384. }
  1385. if (error) {
  1386. ASSERT(committed);
  1387. args->trans = NULL;
  1388. xfs_bmap_cancel(args->flist);
  1389. goto out;
  1390. }
  1391. /*
  1392. * bmap_finish() may have committed the last trans and started
  1393. * a new one. We need the inode to be in all transactions.
  1394. */
  1395. if (committed)
  1396. xfs_trans_ijoin(args->trans, dp);
  1397. /*
  1398. * Commit the Btree join operation and start a new trans.
  1399. */
  1400. error = xfs_trans_roll(&args->trans, dp);
  1401. if (error)
  1402. goto out;
  1403. }
  1404. /*
  1405. * If the result is small enough, push it all into the inode.
  1406. */
  1407. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1408. /*
  1409. * Have to get rid of the copy of this dabuf in the state.
  1410. */
  1411. ASSERT(state->path.active == 1);
  1412. ASSERT(state->path.blk[0].bp);
  1413. xfs_da_buf_done(state->path.blk[0].bp);
  1414. state->path.blk[0].bp = NULL;
  1415. error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
  1416. XFS_ATTR_FORK);
  1417. if (error)
  1418. goto out;
  1419. ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
  1420. bp->data)->hdr.info.magic)
  1421. == XFS_ATTR_LEAF_MAGIC);
  1422. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1423. xfs_bmap_init(args->flist, args->firstblock);
  1424. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1425. /* bp is gone due to xfs_da_shrink_inode */
  1426. if (!error) {
  1427. error = xfs_bmap_finish(&args->trans,
  1428. args->flist,
  1429. &committed);
  1430. }
  1431. if (error) {
  1432. ASSERT(committed);
  1433. args->trans = NULL;
  1434. xfs_bmap_cancel(args->flist);
  1435. goto out;
  1436. }
  1437. /*
  1438. * bmap_finish() may have committed the last trans
  1439. * and started a new one. We need the inode to be
  1440. * in all transactions.
  1441. */
  1442. if (committed)
  1443. xfs_trans_ijoin(args->trans, dp);
  1444. } else
  1445. xfs_da_brelse(args->trans, bp);
  1446. }
  1447. error = 0;
  1448. out:
  1449. xfs_da_state_free(state);
  1450. return(error);
  1451. }
  1452. /*
  1453. * Fill in the disk block numbers in the state structure for the buffers
  1454. * that are attached to the state structure.
  1455. * This is done so that we can quickly reattach ourselves to those buffers
  1456. * after some set of transaction commits have released these buffers.
  1457. */
  1458. STATIC int
  1459. xfs_attr_fillstate(xfs_da_state_t *state)
  1460. {
  1461. xfs_da_state_path_t *path;
  1462. xfs_da_state_blk_t *blk;
  1463. int level;
  1464. /*
  1465. * Roll down the "path" in the state structure, storing the on-disk
  1466. * block number for those buffers in the "path".
  1467. */
  1468. path = &state->path;
  1469. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1470. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1471. if (blk->bp) {
  1472. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1473. xfs_da_buf_done(blk->bp);
  1474. blk->bp = NULL;
  1475. } else {
  1476. blk->disk_blkno = 0;
  1477. }
  1478. }
  1479. /*
  1480. * Roll down the "altpath" in the state structure, storing the on-disk
  1481. * block number for those buffers in the "altpath".
  1482. */
  1483. path = &state->altpath;
  1484. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1485. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1486. if (blk->bp) {
  1487. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1488. xfs_da_buf_done(blk->bp);
  1489. blk->bp = NULL;
  1490. } else {
  1491. blk->disk_blkno = 0;
  1492. }
  1493. }
  1494. return(0);
  1495. }
  1496. /*
  1497. * Reattach the buffers to the state structure based on the disk block
  1498. * numbers stored in the state structure.
  1499. * This is done after some set of transaction commits have released those
  1500. * buffers from our grip.
  1501. */
  1502. STATIC int
  1503. xfs_attr_refillstate(xfs_da_state_t *state)
  1504. {
  1505. xfs_da_state_path_t *path;
  1506. xfs_da_state_blk_t *blk;
  1507. int level, error;
  1508. /*
  1509. * Roll down the "path" in the state structure, storing the on-disk
  1510. * block number for those buffers in the "path".
  1511. */
  1512. path = &state->path;
  1513. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1514. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1515. if (blk->disk_blkno) {
  1516. error = xfs_da_read_buf(state->args->trans,
  1517. state->args->dp,
  1518. blk->blkno, blk->disk_blkno,
  1519. &blk->bp, XFS_ATTR_FORK);
  1520. if (error)
  1521. return(error);
  1522. } else {
  1523. blk->bp = NULL;
  1524. }
  1525. }
  1526. /*
  1527. * Roll down the "altpath" in the state structure, storing the on-disk
  1528. * block number for those buffers in the "altpath".
  1529. */
  1530. path = &state->altpath;
  1531. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1532. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1533. if (blk->disk_blkno) {
  1534. error = xfs_da_read_buf(state->args->trans,
  1535. state->args->dp,
  1536. blk->blkno, blk->disk_blkno,
  1537. &blk->bp, XFS_ATTR_FORK);
  1538. if (error)
  1539. return(error);
  1540. } else {
  1541. blk->bp = NULL;
  1542. }
  1543. }
  1544. return(0);
  1545. }
  1546. /*
  1547. * Look up a filename in a node attribute list.
  1548. *
  1549. * This routine gets called for any attribute fork that has more than one
  1550. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1551. * "remote" values taking up more blocks.
  1552. */
  1553. STATIC int
  1554. xfs_attr_node_get(xfs_da_args_t *args)
  1555. {
  1556. xfs_da_state_t *state;
  1557. xfs_da_state_blk_t *blk;
  1558. int error, retval;
  1559. int i;
  1560. state = xfs_da_state_alloc();
  1561. state->args = args;
  1562. state->mp = args->dp->i_mount;
  1563. state->blocksize = state->mp->m_sb.sb_blocksize;
  1564. state->node_ents = state->mp->m_attr_node_ents;
  1565. /*
  1566. * Search to see if name exists, and get back a pointer to it.
  1567. */
  1568. error = xfs_da_node_lookup_int(state, &retval);
  1569. if (error) {
  1570. retval = error;
  1571. } else if (retval == EEXIST) {
  1572. blk = &state->path.blk[ state->path.active-1 ];
  1573. ASSERT(blk->bp != NULL);
  1574. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1575. /*
  1576. * Get the value, local or "remote"
  1577. */
  1578. retval = xfs_attr_leaf_getvalue(blk->bp, args);
  1579. if (!retval && (args->rmtblkno > 0)
  1580. && !(args->flags & ATTR_KERNOVAL)) {
  1581. retval = xfs_attr_rmtval_get(args);
  1582. }
  1583. }
  1584. /*
  1585. * If not in a transaction, we have to release all the buffers.
  1586. */
  1587. for (i = 0; i < state->path.active; i++) {
  1588. xfs_da_brelse(args->trans, state->path.blk[i].bp);
  1589. state->path.blk[i].bp = NULL;
  1590. }
  1591. xfs_da_state_free(state);
  1592. return(retval);
  1593. }
  1594. STATIC int /* error */
  1595. xfs_attr_node_list(xfs_attr_list_context_t *context)
  1596. {
  1597. attrlist_cursor_kern_t *cursor;
  1598. xfs_attr_leafblock_t *leaf;
  1599. xfs_da_intnode_t *node;
  1600. xfs_da_node_entry_t *btree;
  1601. int error, i;
  1602. xfs_dabuf_t *bp;
  1603. cursor = context->cursor;
  1604. cursor->initted = 1;
  1605. /*
  1606. * Do all sorts of validation on the passed-in cursor structure.
  1607. * If anything is amiss, ignore the cursor and look up the hashval
  1608. * starting from the btree root.
  1609. */
  1610. bp = NULL;
  1611. if (cursor->blkno > 0) {
  1612. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1613. &bp, XFS_ATTR_FORK);
  1614. if ((error != 0) && (error != EFSCORRUPTED))
  1615. return(error);
  1616. if (bp) {
  1617. node = bp->data;
  1618. switch (be16_to_cpu(node->hdr.info.magic)) {
  1619. case XFS_DA_NODE_MAGIC:
  1620. trace_xfs_attr_list_wrong_blk(context);
  1621. xfs_da_brelse(NULL, bp);
  1622. bp = NULL;
  1623. break;
  1624. case XFS_ATTR_LEAF_MAGIC:
  1625. leaf = bp->data;
  1626. if (cursor->hashval > be32_to_cpu(leaf->entries[
  1627. be16_to_cpu(leaf->hdr.count)-1].hashval)) {
  1628. trace_xfs_attr_list_wrong_blk(context);
  1629. xfs_da_brelse(NULL, bp);
  1630. bp = NULL;
  1631. } else if (cursor->hashval <=
  1632. be32_to_cpu(leaf->entries[0].hashval)) {
  1633. trace_xfs_attr_list_wrong_blk(context);
  1634. xfs_da_brelse(NULL, bp);
  1635. bp = NULL;
  1636. }
  1637. break;
  1638. default:
  1639. trace_xfs_attr_list_wrong_blk(context);
  1640. xfs_da_brelse(NULL, bp);
  1641. bp = NULL;
  1642. }
  1643. }
  1644. }
  1645. /*
  1646. * We did not find what we expected given the cursor's contents,
  1647. * so we start from the top and work down based on the hash value.
  1648. * Note that start of node block is same as start of leaf block.
  1649. */
  1650. if (bp == NULL) {
  1651. cursor->blkno = 0;
  1652. for (;;) {
  1653. error = xfs_da_read_buf(NULL, context->dp,
  1654. cursor->blkno, -1, &bp,
  1655. XFS_ATTR_FORK);
  1656. if (error)
  1657. return(error);
  1658. if (unlikely(bp == NULL)) {
  1659. XFS_ERROR_REPORT("xfs_attr_node_list(2)",
  1660. XFS_ERRLEVEL_LOW,
  1661. context->dp->i_mount);
  1662. return(XFS_ERROR(EFSCORRUPTED));
  1663. }
  1664. node = bp->data;
  1665. if (be16_to_cpu(node->hdr.info.magic)
  1666. == XFS_ATTR_LEAF_MAGIC)
  1667. break;
  1668. if (unlikely(be16_to_cpu(node->hdr.info.magic)
  1669. != XFS_DA_NODE_MAGIC)) {
  1670. XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  1671. XFS_ERRLEVEL_LOW,
  1672. context->dp->i_mount,
  1673. node);
  1674. xfs_da_brelse(NULL, bp);
  1675. return(XFS_ERROR(EFSCORRUPTED));
  1676. }
  1677. btree = node->btree;
  1678. for (i = 0; i < be16_to_cpu(node->hdr.count);
  1679. btree++, i++) {
  1680. if (cursor->hashval
  1681. <= be32_to_cpu(btree->hashval)) {
  1682. cursor->blkno = be32_to_cpu(btree->before);
  1683. trace_xfs_attr_list_node_descend(context,
  1684. btree);
  1685. break;
  1686. }
  1687. }
  1688. if (i == be16_to_cpu(node->hdr.count)) {
  1689. xfs_da_brelse(NULL, bp);
  1690. return(0);
  1691. }
  1692. xfs_da_brelse(NULL, bp);
  1693. }
  1694. }
  1695. ASSERT(bp != NULL);
  1696. /*
  1697. * Roll upward through the blocks, processing each leaf block in
  1698. * order. As long as there is space in the result buffer, keep
  1699. * adding the information.
  1700. */
  1701. for (;;) {
  1702. leaf = bp->data;
  1703. if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
  1704. != XFS_ATTR_LEAF_MAGIC)) {
  1705. XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
  1706. XFS_ERRLEVEL_LOW,
  1707. context->dp->i_mount, leaf);
  1708. xfs_da_brelse(NULL, bp);
  1709. return(XFS_ERROR(EFSCORRUPTED));
  1710. }
  1711. error = xfs_attr_leaf_list_int(bp, context);
  1712. if (error) {
  1713. xfs_da_brelse(NULL, bp);
  1714. return error;
  1715. }
  1716. if (context->seen_enough || leaf->hdr.info.forw == 0)
  1717. break;
  1718. cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
  1719. xfs_da_brelse(NULL, bp);
  1720. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1721. &bp, XFS_ATTR_FORK);
  1722. if (error)
  1723. return(error);
  1724. if (unlikely((bp == NULL))) {
  1725. XFS_ERROR_REPORT("xfs_attr_node_list(5)",
  1726. XFS_ERRLEVEL_LOW,
  1727. context->dp->i_mount);
  1728. return(XFS_ERROR(EFSCORRUPTED));
  1729. }
  1730. }
  1731. xfs_da_brelse(NULL, bp);
  1732. return(0);
  1733. }
  1734. /*========================================================================
  1735. * External routines for manipulating out-of-line attribute values.
  1736. *========================================================================*/
  1737. /*
  1738. * Read the value associated with an attribute from the out-of-line buffer
  1739. * that we stored it in.
  1740. */
  1741. int
  1742. xfs_attr_rmtval_get(xfs_da_args_t *args)
  1743. {
  1744. xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
  1745. xfs_mount_t *mp;
  1746. xfs_daddr_t dblkno;
  1747. void *dst;
  1748. xfs_buf_t *bp;
  1749. int nmap, error, tmp, valuelen, blkcnt, i;
  1750. xfs_dablk_t lblkno;
  1751. ASSERT(!(args->flags & ATTR_KERNOVAL));
  1752. mp = args->dp->i_mount;
  1753. dst = args->value;
  1754. valuelen = args->valuelen;
  1755. lblkno = args->rmtblkno;
  1756. while (valuelen > 0) {
  1757. nmap = ATTR_RMTVALUE_MAPSIZE;
  1758. error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
  1759. args->rmtblkcnt,
  1760. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1761. NULL, 0, map, &nmap, NULL);
  1762. if (error)
  1763. return(error);
  1764. ASSERT(nmap >= 1);
  1765. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  1766. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  1767. (map[i].br_startblock != HOLESTARTBLOCK));
  1768. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  1769. blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  1770. error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
  1771. blkcnt, XBF_LOCK | XBF_DONT_BLOCK,
  1772. &bp);
  1773. if (error)
  1774. return(error);
  1775. tmp = (valuelen < XFS_BUF_SIZE(bp))
  1776. ? valuelen : XFS_BUF_SIZE(bp);
  1777. xfs_buf_iomove(bp, 0, tmp, dst, XBRW_READ);
  1778. xfs_buf_relse(bp);
  1779. dst += tmp;
  1780. valuelen -= tmp;
  1781. lblkno += map[i].br_blockcount;
  1782. }
  1783. }
  1784. ASSERT(valuelen == 0);
  1785. return(0);
  1786. }
  1787. /*
  1788. * Write the value associated with an attribute into the out-of-line buffer
  1789. * that we have defined for it.
  1790. */
  1791. STATIC int
  1792. xfs_attr_rmtval_set(xfs_da_args_t *args)
  1793. {
  1794. xfs_mount_t *mp;
  1795. xfs_fileoff_t lfileoff;
  1796. xfs_inode_t *dp;
  1797. xfs_bmbt_irec_t map;
  1798. xfs_daddr_t dblkno;
  1799. void *src;
  1800. xfs_buf_t *bp;
  1801. xfs_dablk_t lblkno;
  1802. int blkcnt, valuelen, nmap, error, tmp, committed;
  1803. dp = args->dp;
  1804. mp = dp->i_mount;
  1805. src = args->value;
  1806. /*
  1807. * Find a "hole" in the attribute address space large enough for
  1808. * us to drop the new attribute's value into.
  1809. */
  1810. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  1811. lfileoff = 0;
  1812. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  1813. XFS_ATTR_FORK);
  1814. if (error) {
  1815. return(error);
  1816. }
  1817. args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
  1818. args->rmtblkcnt = blkcnt;
  1819. /*
  1820. * Roll through the "value", allocating blocks on disk as required.
  1821. */
  1822. while (blkcnt > 0) {
  1823. /*
  1824. * Allocate a single extent, up to the size of the value.
  1825. */
  1826. xfs_bmap_init(args->flist, args->firstblock);
  1827. nmap = 1;
  1828. error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
  1829. blkcnt,
  1830. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
  1831. XFS_BMAPI_WRITE,
  1832. args->firstblock, args->total, &map, &nmap,
  1833. args->flist);
  1834. if (!error) {
  1835. error = xfs_bmap_finish(&args->trans, args->flist,
  1836. &committed);
  1837. }
  1838. if (error) {
  1839. ASSERT(committed);
  1840. args->trans = NULL;
  1841. xfs_bmap_cancel(args->flist);
  1842. return(error);
  1843. }
  1844. /*
  1845. * bmap_finish() may have committed the last trans and started
  1846. * a new one. We need the inode to be in all transactions.
  1847. */
  1848. if (committed)
  1849. xfs_trans_ijoin(args->trans, dp);
  1850. ASSERT(nmap == 1);
  1851. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1852. (map.br_startblock != HOLESTARTBLOCK));
  1853. lblkno += map.br_blockcount;
  1854. blkcnt -= map.br_blockcount;
  1855. /*
  1856. * Start the next trans in the chain.
  1857. */
  1858. error = xfs_trans_roll(&args->trans, dp);
  1859. if (error)
  1860. return (error);
  1861. }
  1862. /*
  1863. * Roll through the "value", copying the attribute value to the
  1864. * already-allocated blocks. Blocks are written synchronously
  1865. * so that we can know they are all on disk before we turn off
  1866. * the INCOMPLETE flag.
  1867. */
  1868. lblkno = args->rmtblkno;
  1869. valuelen = args->valuelen;
  1870. while (valuelen > 0) {
  1871. /*
  1872. * Try to remember where we decided to put the value.
  1873. */
  1874. xfs_bmap_init(args->flist, args->firstblock);
  1875. nmap = 1;
  1876. error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
  1877. args->rmtblkcnt,
  1878. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1879. args->firstblock, 0, &map, &nmap,
  1880. NULL);
  1881. if (error) {
  1882. return(error);
  1883. }
  1884. ASSERT(nmap == 1);
  1885. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1886. (map.br_startblock != HOLESTARTBLOCK));
  1887. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1888. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1889. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt,
  1890. XBF_LOCK | XBF_DONT_BLOCK);
  1891. ASSERT(bp);
  1892. ASSERT(!XFS_BUF_GETERROR(bp));
  1893. tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
  1894. XFS_BUF_SIZE(bp);
  1895. xfs_buf_iomove(bp, 0, tmp, src, XBRW_WRITE);
  1896. if (tmp < XFS_BUF_SIZE(bp))
  1897. xfs_buf_zero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
  1898. if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
  1899. return (error);
  1900. }
  1901. src += tmp;
  1902. valuelen -= tmp;
  1903. lblkno += map.br_blockcount;
  1904. }
  1905. ASSERT(valuelen == 0);
  1906. return(0);
  1907. }
  1908. /*
  1909. * Remove the value associated with an attribute by deleting the
  1910. * out-of-line buffer that it is stored on.
  1911. */
  1912. STATIC int
  1913. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  1914. {
  1915. xfs_mount_t *mp;
  1916. xfs_bmbt_irec_t map;
  1917. xfs_buf_t *bp;
  1918. xfs_daddr_t dblkno;
  1919. xfs_dablk_t lblkno;
  1920. int valuelen, blkcnt, nmap, error, done, committed;
  1921. mp = args->dp->i_mount;
  1922. /*
  1923. * Roll through the "value", invalidating the attribute value's
  1924. * blocks.
  1925. */
  1926. lblkno = args->rmtblkno;
  1927. valuelen = args->rmtblkcnt;
  1928. while (valuelen > 0) {
  1929. /*
  1930. * Try to remember where we decided to put the value.
  1931. */
  1932. xfs_bmap_init(args->flist, args->firstblock);
  1933. nmap = 1;
  1934. error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
  1935. args->rmtblkcnt,
  1936. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1937. args->firstblock, 0, &map, &nmap,
  1938. args->flist);
  1939. if (error) {
  1940. return(error);
  1941. }
  1942. ASSERT(nmap == 1);
  1943. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1944. (map.br_startblock != HOLESTARTBLOCK));
  1945. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1946. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1947. /*
  1948. * If the "remote" value is in the cache, remove it.
  1949. */
  1950. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  1951. if (bp) {
  1952. XFS_BUF_STALE(bp);
  1953. XFS_BUF_UNDELAYWRITE(bp);
  1954. xfs_buf_relse(bp);
  1955. bp = NULL;
  1956. }
  1957. valuelen -= map.br_blockcount;
  1958. lblkno += map.br_blockcount;
  1959. }
  1960. /*
  1961. * Keep de-allocating extents until the remote-value region is gone.
  1962. */
  1963. lblkno = args->rmtblkno;
  1964. blkcnt = args->rmtblkcnt;
  1965. done = 0;
  1966. while (!done) {
  1967. xfs_bmap_init(args->flist, args->firstblock);
  1968. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  1969. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1970. 1, args->firstblock, args->flist,
  1971. &done);
  1972. if (!error) {
  1973. error = xfs_bmap_finish(&args->trans, args->flist,
  1974. &committed);
  1975. }
  1976. if (error) {
  1977. ASSERT(committed);
  1978. args->trans = NULL;
  1979. xfs_bmap_cancel(args->flist);
  1980. return(error);
  1981. }
  1982. /*
  1983. * bmap_finish() may have committed the last trans and started
  1984. * a new one. We need the inode to be in all transactions.
  1985. */
  1986. if (committed)
  1987. xfs_trans_ijoin(args->trans, args->dp);
  1988. /*
  1989. * Close out trans and start the next one in the chain.
  1990. */
  1991. error = xfs_trans_roll(&args->trans, args->dp);
  1992. if (error)
  1993. return (error);
  1994. }
  1995. return(0);
  1996. }