xfs_attr.c 59 KB

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