xfs_attr.c 55 KB

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