xfs_attr.c 59 KB

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