xfs_attr.c 47 KB

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