xfs_attr.c 55 KB

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