xfs_attr.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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. xfs_dabuf_t *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_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. 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. xfs_da_buf_done(bp);
  811. if (retval == ENOSPC) {
  812. /*
  813. * Promote the attribute list to the Btree format, then
  814. * Commit that transaction so that the node_addname() call
  815. * can manage its own transactions.
  816. */
  817. xfs_bmap_init(args->flist, args->firstblock);
  818. error = xfs_attr_leaf_to_node(args);
  819. if (!error) {
  820. error = xfs_bmap_finish(&args->trans, args->flist,
  821. &committed);
  822. }
  823. if (error) {
  824. ASSERT(committed);
  825. args->trans = NULL;
  826. xfs_bmap_cancel(args->flist);
  827. return(error);
  828. }
  829. /*
  830. * bmap_finish() may have committed the last trans and started
  831. * a new one. We need the inode to be in all transactions.
  832. */
  833. if (committed)
  834. xfs_trans_ijoin(args->trans, dp, 0);
  835. /*
  836. * Commit the current trans (including the inode) and start
  837. * a new one.
  838. */
  839. error = xfs_trans_roll(&args->trans, dp);
  840. if (error)
  841. return (error);
  842. /*
  843. * Fob the whole rest of the problem off on the Btree code.
  844. */
  845. error = xfs_attr_node_addname(args);
  846. return(error);
  847. }
  848. /*
  849. * Commit the transaction that added the attr name so that
  850. * later routines can manage their own transactions.
  851. */
  852. error = xfs_trans_roll(&args->trans, dp);
  853. if (error)
  854. return (error);
  855. /*
  856. * If there was an out-of-line value, allocate the blocks we
  857. * identified for its storage and copy the value. This is done
  858. * after we create the attribute so that we don't overflow the
  859. * maximum size of a transaction and/or hit a deadlock.
  860. */
  861. if (args->rmtblkno > 0) {
  862. error = xfs_attr_rmtval_set(args);
  863. if (error)
  864. return(error);
  865. }
  866. /*
  867. * If this is an atomic rename operation, we must "flip" the
  868. * incomplete flags on the "new" and "old" attribute/value pairs
  869. * so that one disappears and one appears atomically. Then we
  870. * must remove the "old" attribute/value pair.
  871. */
  872. if (args->op_flags & XFS_DA_OP_RENAME) {
  873. /*
  874. * In a separate transaction, set the incomplete flag on the
  875. * "old" attr and clear the incomplete flag on the "new" attr.
  876. */
  877. error = xfs_attr_leaf_flipflags(args);
  878. if (error)
  879. return(error);
  880. /*
  881. * Dismantle the "old" attribute/value pair by removing
  882. * a "remote" value (if it exists).
  883. */
  884. args->index = args->index2;
  885. args->blkno = args->blkno2;
  886. args->rmtblkno = args->rmtblkno2;
  887. args->rmtblkcnt = args->rmtblkcnt2;
  888. if (args->rmtblkno) {
  889. error = xfs_attr_rmtval_remove(args);
  890. if (error)
  891. return(error);
  892. }
  893. /*
  894. * Read in the block containing the "old" attr, then
  895. * remove the "old" attr from that block (neat, huh!)
  896. */
  897. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
  898. &bp, XFS_ATTR_FORK);
  899. if (error)
  900. return(error);
  901. ASSERT(bp != NULL);
  902. (void)xfs_attr_leaf_remove(bp, args);
  903. /*
  904. * If the result is small enough, shrink it all into the inode.
  905. */
  906. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  907. xfs_bmap_init(args->flist, args->firstblock);
  908. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  909. /* bp is gone due to xfs_da_shrink_inode */
  910. if (!error) {
  911. error = xfs_bmap_finish(&args->trans,
  912. args->flist,
  913. &committed);
  914. }
  915. if (error) {
  916. ASSERT(committed);
  917. args->trans = NULL;
  918. xfs_bmap_cancel(args->flist);
  919. return(error);
  920. }
  921. /*
  922. * bmap_finish() may have committed the last trans
  923. * and started a new one. We need the inode to be
  924. * in all transactions.
  925. */
  926. if (committed)
  927. xfs_trans_ijoin(args->trans, dp, 0);
  928. } else
  929. xfs_da_buf_done(bp);
  930. /*
  931. * Commit the remove and start the next trans in series.
  932. */
  933. error = xfs_trans_roll(&args->trans, dp);
  934. } else if (args->rmtblkno > 0) {
  935. /*
  936. * Added a "remote" value, just clear the incomplete flag.
  937. */
  938. error = xfs_attr_leaf_clearflag(args);
  939. }
  940. return(error);
  941. }
  942. /*
  943. * Remove a name from the leaf attribute list structure
  944. *
  945. * This leaf block cannot have a "remote" value, we only call this routine
  946. * if bmap_one_block() says there is only one block (ie: no remote blks).
  947. */
  948. STATIC int
  949. xfs_attr_leaf_removename(xfs_da_args_t *args)
  950. {
  951. xfs_inode_t *dp;
  952. xfs_dabuf_t *bp;
  953. int error, committed, forkoff;
  954. trace_xfs_attr_leaf_removename(args);
  955. /*
  956. * Remove the attribute.
  957. */
  958. dp = args->dp;
  959. args->blkno = 0;
  960. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  961. XFS_ATTR_FORK);
  962. if (error) {
  963. return(error);
  964. }
  965. ASSERT(bp != NULL);
  966. error = xfs_attr_leaf_lookup_int(bp, args);
  967. if (error == ENOATTR) {
  968. xfs_da_brelse(args->trans, bp);
  969. return(error);
  970. }
  971. (void)xfs_attr_leaf_remove(bp, args);
  972. /*
  973. * If the result is small enough, shrink it all into the inode.
  974. */
  975. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  976. xfs_bmap_init(args->flist, args->firstblock);
  977. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  978. /* bp is gone due to xfs_da_shrink_inode */
  979. if (!error) {
  980. error = xfs_bmap_finish(&args->trans, args->flist,
  981. &committed);
  982. }
  983. if (error) {
  984. ASSERT(committed);
  985. args->trans = NULL;
  986. xfs_bmap_cancel(args->flist);
  987. return(error);
  988. }
  989. /*
  990. * bmap_finish() may have committed the last trans and started
  991. * a new one. We need the inode to be in all transactions.
  992. */
  993. if (committed)
  994. xfs_trans_ijoin(args->trans, dp, 0);
  995. } else
  996. xfs_da_buf_done(bp);
  997. return(0);
  998. }
  999. /*
  1000. * Look up a name in a leaf attribute list structure.
  1001. *
  1002. * This leaf block cannot have a "remote" value, we only call this routine
  1003. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1004. */
  1005. STATIC int
  1006. xfs_attr_leaf_get(xfs_da_args_t *args)
  1007. {
  1008. xfs_dabuf_t *bp;
  1009. int error;
  1010. args->blkno = 0;
  1011. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  1012. XFS_ATTR_FORK);
  1013. if (error)
  1014. return(error);
  1015. ASSERT(bp != NULL);
  1016. error = xfs_attr_leaf_lookup_int(bp, args);
  1017. if (error != EEXIST) {
  1018. xfs_da_brelse(args->trans, bp);
  1019. return(error);
  1020. }
  1021. error = xfs_attr_leaf_getvalue(bp, args);
  1022. xfs_da_brelse(args->trans, bp);
  1023. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  1024. error = xfs_attr_rmtval_get(args);
  1025. }
  1026. return(error);
  1027. }
  1028. /*
  1029. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  1030. */
  1031. STATIC int
  1032. xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  1033. {
  1034. xfs_attr_leafblock_t *leaf;
  1035. int error;
  1036. xfs_dabuf_t *bp;
  1037. context->cursor->blkno = 0;
  1038. error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
  1039. if (error)
  1040. return XFS_ERROR(error);
  1041. ASSERT(bp != NULL);
  1042. leaf = bp->data;
  1043. if (unlikely(leaf->hdr.info.magic != cpu_to_be16(XFS_ATTR_LEAF_MAGIC))) {
  1044. XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
  1045. context->dp->i_mount, leaf);
  1046. xfs_da_brelse(NULL, bp);
  1047. return XFS_ERROR(EFSCORRUPTED);
  1048. }
  1049. error = xfs_attr_leaf_list_int(bp, context);
  1050. xfs_da_brelse(NULL, bp);
  1051. return XFS_ERROR(error);
  1052. }
  1053. /*========================================================================
  1054. * External routines when attribute list size > XFS_LBSIZE(mp).
  1055. *========================================================================*/
  1056. /*
  1057. * Add a name to a Btree-format attribute list.
  1058. *
  1059. * This will involve walking down the Btree, and may involve splitting
  1060. * leaf nodes and even splitting intermediate nodes up to and including
  1061. * the root node (a special case of an intermediate node).
  1062. *
  1063. * "Remote" attribute values confuse the issue and atomic rename operations
  1064. * add a whole extra layer of confusion on top of that.
  1065. */
  1066. STATIC int
  1067. xfs_attr_node_addname(xfs_da_args_t *args)
  1068. {
  1069. xfs_da_state_t *state;
  1070. xfs_da_state_blk_t *blk;
  1071. xfs_inode_t *dp;
  1072. xfs_mount_t *mp;
  1073. int committed, retval, error;
  1074. trace_xfs_attr_node_addname(args);
  1075. /*
  1076. * Fill in bucket of arguments/results/context to carry around.
  1077. */
  1078. dp = args->dp;
  1079. mp = dp->i_mount;
  1080. restart:
  1081. state = xfs_da_state_alloc();
  1082. state->args = args;
  1083. state->mp = mp;
  1084. state->blocksize = state->mp->m_sb.sb_blocksize;
  1085. state->node_ents = state->mp->m_attr_node_ents;
  1086. /*
  1087. * Search to see if name already exists, and get back a pointer
  1088. * to where it should go.
  1089. */
  1090. error = xfs_da_node_lookup_int(state, &retval);
  1091. if (error)
  1092. goto out;
  1093. blk = &state->path.blk[ state->path.active-1 ];
  1094. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1095. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  1096. goto out;
  1097. } else if (retval == EEXIST) {
  1098. if (args->flags & ATTR_CREATE)
  1099. goto out;
  1100. trace_xfs_attr_node_replace(args);
  1101. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  1102. args->blkno2 = args->blkno; /* set 2nd entry info*/
  1103. args->index2 = args->index;
  1104. args->rmtblkno2 = args->rmtblkno;
  1105. args->rmtblkcnt2 = args->rmtblkcnt;
  1106. args->rmtblkno = 0;
  1107. args->rmtblkcnt = 0;
  1108. }
  1109. retval = xfs_attr_leaf_add(blk->bp, state->args);
  1110. if (retval == ENOSPC) {
  1111. if (state->path.active == 1) {
  1112. /*
  1113. * Its really a single leaf node, but it had
  1114. * out-of-line values so it looked like it *might*
  1115. * have been a b-tree.
  1116. */
  1117. xfs_da_state_free(state);
  1118. xfs_bmap_init(args->flist, args->firstblock);
  1119. error = xfs_attr_leaf_to_node(args);
  1120. if (!error) {
  1121. error = xfs_bmap_finish(&args->trans,
  1122. args->flist,
  1123. &committed);
  1124. }
  1125. if (error) {
  1126. ASSERT(committed);
  1127. args->trans = NULL;
  1128. xfs_bmap_cancel(args->flist);
  1129. goto out;
  1130. }
  1131. /*
  1132. * bmap_finish() may have committed the last trans
  1133. * and started a new one. We need the inode to be
  1134. * in all transactions.
  1135. */
  1136. if (committed)
  1137. xfs_trans_ijoin(args->trans, dp, 0);
  1138. /*
  1139. * Commit the node conversion and start the next
  1140. * trans in the chain.
  1141. */
  1142. error = xfs_trans_roll(&args->trans, dp);
  1143. if (error)
  1144. goto out;
  1145. goto restart;
  1146. }
  1147. /*
  1148. * Split as many Btree elements as required.
  1149. * This code tracks the new and old attr's location
  1150. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1151. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1152. */
  1153. xfs_bmap_init(args->flist, args->firstblock);
  1154. error = xfs_da_split(state);
  1155. if (!error) {
  1156. error = xfs_bmap_finish(&args->trans, args->flist,
  1157. &committed);
  1158. }
  1159. if (error) {
  1160. ASSERT(committed);
  1161. args->trans = NULL;
  1162. xfs_bmap_cancel(args->flist);
  1163. goto out;
  1164. }
  1165. /*
  1166. * bmap_finish() may have committed the last trans and started
  1167. * a new one. We need the inode to be in all transactions.
  1168. */
  1169. if (committed)
  1170. xfs_trans_ijoin(args->trans, dp, 0);
  1171. } else {
  1172. /*
  1173. * Addition succeeded, update Btree hashvals.
  1174. */
  1175. xfs_da_fixhashpath(state, &state->path);
  1176. }
  1177. /*
  1178. * Kill the state structure, we're done with it and need to
  1179. * allow the buffers to come back later.
  1180. */
  1181. xfs_da_state_free(state);
  1182. state = NULL;
  1183. /*
  1184. * Commit the leaf addition or btree split and start the next
  1185. * trans in the chain.
  1186. */
  1187. error = xfs_trans_roll(&args->trans, dp);
  1188. if (error)
  1189. goto out;
  1190. /*
  1191. * If there was an out-of-line value, allocate the blocks we
  1192. * identified for its storage and copy the value. This is done
  1193. * after we create the attribute so that we don't overflow the
  1194. * maximum size of a transaction and/or hit a deadlock.
  1195. */
  1196. if (args->rmtblkno > 0) {
  1197. error = xfs_attr_rmtval_set(args);
  1198. if (error)
  1199. return(error);
  1200. }
  1201. /*
  1202. * If this is an atomic rename operation, we must "flip" the
  1203. * incomplete flags on the "new" and "old" attribute/value pairs
  1204. * so that one disappears and one appears atomically. Then we
  1205. * must remove the "old" attribute/value pair.
  1206. */
  1207. if (args->op_flags & XFS_DA_OP_RENAME) {
  1208. /*
  1209. * In a separate transaction, set the incomplete flag on the
  1210. * "old" attr and clear the incomplete flag on the "new" attr.
  1211. */
  1212. error = xfs_attr_leaf_flipflags(args);
  1213. if (error)
  1214. goto out;
  1215. /*
  1216. * Dismantle the "old" attribute/value pair by removing
  1217. * a "remote" value (if it exists).
  1218. */
  1219. args->index = args->index2;
  1220. args->blkno = args->blkno2;
  1221. args->rmtblkno = args->rmtblkno2;
  1222. args->rmtblkcnt = args->rmtblkcnt2;
  1223. if (args->rmtblkno) {
  1224. error = xfs_attr_rmtval_remove(args);
  1225. if (error)
  1226. return(error);
  1227. }
  1228. /*
  1229. * Re-find the "old" attribute entry after any split ops.
  1230. * The INCOMPLETE flag means that we will find the "old"
  1231. * attr, not the "new" one.
  1232. */
  1233. args->flags |= XFS_ATTR_INCOMPLETE;
  1234. state = xfs_da_state_alloc();
  1235. state->args = args;
  1236. state->mp = mp;
  1237. state->blocksize = state->mp->m_sb.sb_blocksize;
  1238. state->node_ents = state->mp->m_attr_node_ents;
  1239. state->inleaf = 0;
  1240. error = xfs_da_node_lookup_int(state, &retval);
  1241. if (error)
  1242. goto out;
  1243. /*
  1244. * Remove the name and update the hashvals in the tree.
  1245. */
  1246. blk = &state->path.blk[ state->path.active-1 ];
  1247. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1248. error = xfs_attr_leaf_remove(blk->bp, args);
  1249. xfs_da_fixhashpath(state, &state->path);
  1250. /*
  1251. * Check to see if the tree needs to be collapsed.
  1252. */
  1253. if (retval && (state->path.active > 1)) {
  1254. xfs_bmap_init(args->flist, args->firstblock);
  1255. error = xfs_da_join(state);
  1256. if (!error) {
  1257. error = xfs_bmap_finish(&args->trans,
  1258. args->flist,
  1259. &committed);
  1260. }
  1261. if (error) {
  1262. ASSERT(committed);
  1263. args->trans = NULL;
  1264. xfs_bmap_cancel(args->flist);
  1265. goto out;
  1266. }
  1267. /*
  1268. * bmap_finish() may have committed the last trans
  1269. * and started a new one. We need the inode to be
  1270. * in all transactions.
  1271. */
  1272. if (committed)
  1273. xfs_trans_ijoin(args->trans, dp, 0);
  1274. }
  1275. /*
  1276. * Commit and start the next trans in the chain.
  1277. */
  1278. error = xfs_trans_roll(&args->trans, dp);
  1279. if (error)
  1280. goto out;
  1281. } else if (args->rmtblkno > 0) {
  1282. /*
  1283. * Added a "remote" value, just clear the incomplete flag.
  1284. */
  1285. error = xfs_attr_leaf_clearflag(args);
  1286. if (error)
  1287. goto out;
  1288. }
  1289. retval = error = 0;
  1290. out:
  1291. if (state)
  1292. xfs_da_state_free(state);
  1293. if (error)
  1294. return(error);
  1295. return(retval);
  1296. }
  1297. /*
  1298. * Remove a name from a B-tree attribute list.
  1299. *
  1300. * This will involve walking down the Btree, and may involve joining
  1301. * leaf nodes and even joining intermediate nodes up to and including
  1302. * the root node (a special case of an intermediate node).
  1303. */
  1304. STATIC int
  1305. xfs_attr_node_removename(xfs_da_args_t *args)
  1306. {
  1307. xfs_da_state_t *state;
  1308. xfs_da_state_blk_t *blk;
  1309. xfs_inode_t *dp;
  1310. xfs_dabuf_t *bp;
  1311. int retval, error, committed, forkoff;
  1312. trace_xfs_attr_node_removename(args);
  1313. /*
  1314. * Tie a string around our finger to remind us where we are.
  1315. */
  1316. dp = args->dp;
  1317. state = xfs_da_state_alloc();
  1318. state->args = args;
  1319. state->mp = dp->i_mount;
  1320. state->blocksize = state->mp->m_sb.sb_blocksize;
  1321. state->node_ents = state->mp->m_attr_node_ents;
  1322. /*
  1323. * Search to see if name exists, and get back a pointer to it.
  1324. */
  1325. error = xfs_da_node_lookup_int(state, &retval);
  1326. if (error || (retval != EEXIST)) {
  1327. if (error == 0)
  1328. error = retval;
  1329. goto out;
  1330. }
  1331. /*
  1332. * If there is an out-of-line value, de-allocate the blocks.
  1333. * This is done before we remove the attribute so that we don't
  1334. * overflow the maximum size of a transaction and/or hit a deadlock.
  1335. */
  1336. blk = &state->path.blk[ state->path.active-1 ];
  1337. ASSERT(blk->bp != NULL);
  1338. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1339. if (args->rmtblkno > 0) {
  1340. /*
  1341. * Fill in disk block numbers in the state structure
  1342. * so that we can get the buffers back after we commit
  1343. * several transactions in the following calls.
  1344. */
  1345. error = xfs_attr_fillstate(state);
  1346. if (error)
  1347. goto out;
  1348. /*
  1349. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1350. * remote value.
  1351. */
  1352. error = xfs_attr_leaf_setflag(args);
  1353. if (error)
  1354. goto out;
  1355. error = xfs_attr_rmtval_remove(args);
  1356. if (error)
  1357. goto out;
  1358. /*
  1359. * Refill the state structure with buffers, the prior calls
  1360. * released our buffers.
  1361. */
  1362. error = xfs_attr_refillstate(state);
  1363. if (error)
  1364. goto out;
  1365. }
  1366. /*
  1367. * Remove the name and update the hashvals in the tree.
  1368. */
  1369. blk = &state->path.blk[ state->path.active-1 ];
  1370. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1371. retval = xfs_attr_leaf_remove(blk->bp, args);
  1372. xfs_da_fixhashpath(state, &state->path);
  1373. /*
  1374. * Check to see if the tree needs to be collapsed.
  1375. */
  1376. if (retval && (state->path.active > 1)) {
  1377. xfs_bmap_init(args->flist, args->firstblock);
  1378. error = xfs_da_join(state);
  1379. if (!error) {
  1380. error = xfs_bmap_finish(&args->trans, args->flist,
  1381. &committed);
  1382. }
  1383. if (error) {
  1384. ASSERT(committed);
  1385. args->trans = NULL;
  1386. xfs_bmap_cancel(args->flist);
  1387. goto out;
  1388. }
  1389. /*
  1390. * bmap_finish() may have committed the last trans and started
  1391. * a new one. We need the inode to be in all transactions.
  1392. */
  1393. if (committed)
  1394. xfs_trans_ijoin(args->trans, dp, 0);
  1395. /*
  1396. * Commit the Btree join operation and start a new trans.
  1397. */
  1398. error = xfs_trans_roll(&args->trans, dp);
  1399. if (error)
  1400. goto out;
  1401. }
  1402. /*
  1403. * If the result is small enough, push it all into the inode.
  1404. */
  1405. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1406. /*
  1407. * Have to get rid of the copy of this dabuf in the state.
  1408. */
  1409. ASSERT(state->path.active == 1);
  1410. ASSERT(state->path.blk[0].bp);
  1411. xfs_da_buf_done(state->path.blk[0].bp);
  1412. state->path.blk[0].bp = NULL;
  1413. error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
  1414. XFS_ATTR_FORK);
  1415. if (error)
  1416. goto out;
  1417. ASSERT((((xfs_attr_leafblock_t *)bp->data)->hdr.info.magic) ==
  1418. cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
  1419. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1420. xfs_bmap_init(args->flist, args->firstblock);
  1421. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1422. /* bp is gone due to xfs_da_shrink_inode */
  1423. if (!error) {
  1424. error = xfs_bmap_finish(&args->trans,
  1425. args->flist,
  1426. &committed);
  1427. }
  1428. if (error) {
  1429. ASSERT(committed);
  1430. args->trans = NULL;
  1431. xfs_bmap_cancel(args->flist);
  1432. goto out;
  1433. }
  1434. /*
  1435. * bmap_finish() may have committed the last trans
  1436. * and started a new one. We need the inode to be
  1437. * in all transactions.
  1438. */
  1439. if (committed)
  1440. xfs_trans_ijoin(args->trans, dp, 0);
  1441. } else
  1442. xfs_da_brelse(args->trans, bp);
  1443. }
  1444. error = 0;
  1445. out:
  1446. xfs_da_state_free(state);
  1447. return(error);
  1448. }
  1449. /*
  1450. * Fill in the disk block numbers in the state structure for the buffers
  1451. * that are attached to the state structure.
  1452. * This is done so that we can quickly reattach ourselves to those buffers
  1453. * after some set of transaction commits have released these buffers.
  1454. */
  1455. STATIC int
  1456. xfs_attr_fillstate(xfs_da_state_t *state)
  1457. {
  1458. xfs_da_state_path_t *path;
  1459. xfs_da_state_blk_t *blk;
  1460. int level;
  1461. /*
  1462. * Roll down the "path" in the state structure, storing the on-disk
  1463. * block number for those buffers in the "path".
  1464. */
  1465. path = &state->path;
  1466. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1467. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1468. if (blk->bp) {
  1469. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1470. xfs_da_buf_done(blk->bp);
  1471. blk->bp = NULL;
  1472. } else {
  1473. blk->disk_blkno = 0;
  1474. }
  1475. }
  1476. /*
  1477. * Roll down the "altpath" in the state structure, storing the on-disk
  1478. * block number for those buffers in the "altpath".
  1479. */
  1480. path = &state->altpath;
  1481. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1482. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1483. if (blk->bp) {
  1484. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1485. xfs_da_buf_done(blk->bp);
  1486. blk->bp = NULL;
  1487. } else {
  1488. blk->disk_blkno = 0;
  1489. }
  1490. }
  1491. return(0);
  1492. }
  1493. /*
  1494. * Reattach the buffers to the state structure based on the disk block
  1495. * numbers stored in the state structure.
  1496. * This is done after some set of transaction commits have released those
  1497. * buffers from our grip.
  1498. */
  1499. STATIC int
  1500. xfs_attr_refillstate(xfs_da_state_t *state)
  1501. {
  1502. xfs_da_state_path_t *path;
  1503. xfs_da_state_blk_t *blk;
  1504. int level, error;
  1505. /*
  1506. * Roll down the "path" in the state structure, storing the on-disk
  1507. * block number for those buffers in the "path".
  1508. */
  1509. path = &state->path;
  1510. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1511. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1512. if (blk->disk_blkno) {
  1513. error = xfs_da_read_buf(state->args->trans,
  1514. state->args->dp,
  1515. blk->blkno, blk->disk_blkno,
  1516. &blk->bp, XFS_ATTR_FORK);
  1517. if (error)
  1518. return(error);
  1519. } else {
  1520. blk->bp = NULL;
  1521. }
  1522. }
  1523. /*
  1524. * Roll down the "altpath" in the state structure, storing the on-disk
  1525. * block number for those buffers in the "altpath".
  1526. */
  1527. path = &state->altpath;
  1528. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1529. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1530. if (blk->disk_blkno) {
  1531. error = xfs_da_read_buf(state->args->trans,
  1532. state->args->dp,
  1533. blk->blkno, blk->disk_blkno,
  1534. &blk->bp, XFS_ATTR_FORK);
  1535. if (error)
  1536. return(error);
  1537. } else {
  1538. blk->bp = NULL;
  1539. }
  1540. }
  1541. return(0);
  1542. }
  1543. /*
  1544. * Look up a filename in a node attribute list.
  1545. *
  1546. * This routine gets called for any attribute fork that has more than one
  1547. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1548. * "remote" values taking up more blocks.
  1549. */
  1550. STATIC int
  1551. xfs_attr_node_get(xfs_da_args_t *args)
  1552. {
  1553. xfs_da_state_t *state;
  1554. xfs_da_state_blk_t *blk;
  1555. int error, retval;
  1556. int i;
  1557. state = xfs_da_state_alloc();
  1558. state->args = args;
  1559. state->mp = args->dp->i_mount;
  1560. state->blocksize = state->mp->m_sb.sb_blocksize;
  1561. state->node_ents = state->mp->m_attr_node_ents;
  1562. /*
  1563. * Search to see if name exists, and get back a pointer to it.
  1564. */
  1565. error = xfs_da_node_lookup_int(state, &retval);
  1566. if (error) {
  1567. retval = error;
  1568. } else if (retval == EEXIST) {
  1569. blk = &state->path.blk[ state->path.active-1 ];
  1570. ASSERT(blk->bp != NULL);
  1571. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1572. /*
  1573. * Get the value, local or "remote"
  1574. */
  1575. retval = xfs_attr_leaf_getvalue(blk->bp, args);
  1576. if (!retval && (args->rmtblkno > 0)
  1577. && !(args->flags & ATTR_KERNOVAL)) {
  1578. retval = xfs_attr_rmtval_get(args);
  1579. }
  1580. }
  1581. /*
  1582. * If not in a transaction, we have to release all the buffers.
  1583. */
  1584. for (i = 0; i < state->path.active; i++) {
  1585. xfs_da_brelse(args->trans, state->path.blk[i].bp);
  1586. state->path.blk[i].bp = NULL;
  1587. }
  1588. xfs_da_state_free(state);
  1589. return(retval);
  1590. }
  1591. STATIC int /* error */
  1592. xfs_attr_node_list(xfs_attr_list_context_t *context)
  1593. {
  1594. attrlist_cursor_kern_t *cursor;
  1595. xfs_attr_leafblock_t *leaf;
  1596. xfs_da_intnode_t *node;
  1597. xfs_da_node_entry_t *btree;
  1598. int error, i;
  1599. xfs_dabuf_t *bp;
  1600. cursor = context->cursor;
  1601. cursor->initted = 1;
  1602. /*
  1603. * Do all sorts of validation on the passed-in cursor structure.
  1604. * If anything is amiss, ignore the cursor and look up the hashval
  1605. * starting from the btree root.
  1606. */
  1607. bp = NULL;
  1608. if (cursor->blkno > 0) {
  1609. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1610. &bp, XFS_ATTR_FORK);
  1611. if ((error != 0) && (error != EFSCORRUPTED))
  1612. return(error);
  1613. if (bp) {
  1614. node = bp->data;
  1615. switch (be16_to_cpu(node->hdr.info.magic)) {
  1616. case XFS_DA_NODE_MAGIC:
  1617. trace_xfs_attr_list_wrong_blk(context);
  1618. xfs_da_brelse(NULL, bp);
  1619. bp = NULL;
  1620. break;
  1621. case XFS_ATTR_LEAF_MAGIC:
  1622. leaf = bp->data;
  1623. if (cursor->hashval > be32_to_cpu(leaf->entries[
  1624. be16_to_cpu(leaf->hdr.count)-1].hashval)) {
  1625. trace_xfs_attr_list_wrong_blk(context);
  1626. xfs_da_brelse(NULL, bp);
  1627. bp = NULL;
  1628. } else if (cursor->hashval <=
  1629. be32_to_cpu(leaf->entries[0].hashval)) {
  1630. trace_xfs_attr_list_wrong_blk(context);
  1631. xfs_da_brelse(NULL, bp);
  1632. bp = NULL;
  1633. }
  1634. break;
  1635. default:
  1636. trace_xfs_attr_list_wrong_blk(context);
  1637. xfs_da_brelse(NULL, bp);
  1638. bp = NULL;
  1639. }
  1640. }
  1641. }
  1642. /*
  1643. * We did not find what we expected given the cursor's contents,
  1644. * so we start from the top and work down based on the hash value.
  1645. * Note that start of node block is same as start of leaf block.
  1646. */
  1647. if (bp == NULL) {
  1648. cursor->blkno = 0;
  1649. for (;;) {
  1650. error = xfs_da_read_buf(NULL, context->dp,
  1651. cursor->blkno, -1, &bp,
  1652. XFS_ATTR_FORK);
  1653. if (error)
  1654. return(error);
  1655. if (unlikely(bp == NULL)) {
  1656. XFS_ERROR_REPORT("xfs_attr_node_list(2)",
  1657. XFS_ERRLEVEL_LOW,
  1658. context->dp->i_mount);
  1659. return(XFS_ERROR(EFSCORRUPTED));
  1660. }
  1661. node = bp->data;
  1662. if (node->hdr.info.magic ==
  1663. cpu_to_be16(XFS_ATTR_LEAF_MAGIC))
  1664. break;
  1665. if (unlikely(node->hdr.info.magic !=
  1666. cpu_to_be16(XFS_DA_NODE_MAGIC))) {
  1667. XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  1668. XFS_ERRLEVEL_LOW,
  1669. context->dp->i_mount,
  1670. node);
  1671. xfs_da_brelse(NULL, bp);
  1672. return(XFS_ERROR(EFSCORRUPTED));
  1673. }
  1674. btree = node->btree;
  1675. for (i = 0; i < be16_to_cpu(node->hdr.count);
  1676. btree++, i++) {
  1677. if (cursor->hashval
  1678. <= be32_to_cpu(btree->hashval)) {
  1679. cursor->blkno = be32_to_cpu(btree->before);
  1680. trace_xfs_attr_list_node_descend(context,
  1681. btree);
  1682. break;
  1683. }
  1684. }
  1685. if (i == be16_to_cpu(node->hdr.count)) {
  1686. xfs_da_brelse(NULL, bp);
  1687. return(0);
  1688. }
  1689. xfs_da_brelse(NULL, bp);
  1690. }
  1691. }
  1692. ASSERT(bp != NULL);
  1693. /*
  1694. * Roll upward through the blocks, processing each leaf block in
  1695. * order. As long as there is space in the result buffer, keep
  1696. * adding the information.
  1697. */
  1698. for (;;) {
  1699. leaf = bp->data;
  1700. if (unlikely(leaf->hdr.info.magic !=
  1701. cpu_to_be16(XFS_ATTR_LEAF_MAGIC))) {
  1702. XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
  1703. XFS_ERRLEVEL_LOW,
  1704. context->dp->i_mount, leaf);
  1705. xfs_da_brelse(NULL, bp);
  1706. return(XFS_ERROR(EFSCORRUPTED));
  1707. }
  1708. error = xfs_attr_leaf_list_int(bp, context);
  1709. if (error) {
  1710. xfs_da_brelse(NULL, bp);
  1711. return error;
  1712. }
  1713. if (context->seen_enough || leaf->hdr.info.forw == 0)
  1714. break;
  1715. cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
  1716. xfs_da_brelse(NULL, bp);
  1717. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1718. &bp, XFS_ATTR_FORK);
  1719. if (error)
  1720. return(error);
  1721. if (unlikely((bp == NULL))) {
  1722. XFS_ERROR_REPORT("xfs_attr_node_list(5)",
  1723. XFS_ERRLEVEL_LOW,
  1724. context->dp->i_mount);
  1725. return(XFS_ERROR(EFSCORRUPTED));
  1726. }
  1727. }
  1728. xfs_da_brelse(NULL, bp);
  1729. return(0);
  1730. }
  1731. /*========================================================================
  1732. * External routines for manipulating out-of-line attribute values.
  1733. *========================================================================*/
  1734. /*
  1735. * Read the value associated with an attribute from the out-of-line buffer
  1736. * that we stored it in.
  1737. */
  1738. int
  1739. xfs_attr_rmtval_get(xfs_da_args_t *args)
  1740. {
  1741. xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
  1742. xfs_mount_t *mp;
  1743. xfs_daddr_t dblkno;
  1744. void *dst;
  1745. xfs_buf_t *bp;
  1746. int nmap, error, tmp, valuelen, blkcnt, i;
  1747. xfs_dablk_t lblkno;
  1748. ASSERT(!(args->flags & ATTR_KERNOVAL));
  1749. mp = args->dp->i_mount;
  1750. dst = args->value;
  1751. valuelen = args->valuelen;
  1752. lblkno = args->rmtblkno;
  1753. while (valuelen > 0) {
  1754. nmap = ATTR_RMTVALUE_MAPSIZE;
  1755. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  1756. args->rmtblkcnt, map, &nmap,
  1757. XFS_BMAPI_ATTRFORK);
  1758. if (error)
  1759. return(error);
  1760. ASSERT(nmap >= 1);
  1761. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  1762. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  1763. (map[i].br_startblock != HOLESTARTBLOCK));
  1764. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  1765. blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  1766. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  1767. dblkno, blkcnt, 0, &bp);
  1768. if (error)
  1769. return(error);
  1770. tmp = min_t(int, valuelen, BBTOB(bp->b_length));
  1771. xfs_buf_iomove(bp, 0, tmp, dst, XBRW_READ);
  1772. xfs_buf_relse(bp);
  1773. dst += tmp;
  1774. valuelen -= tmp;
  1775. lblkno += map[i].br_blockcount;
  1776. }
  1777. }
  1778. ASSERT(valuelen == 0);
  1779. return(0);
  1780. }
  1781. /*
  1782. * Write the value associated with an attribute into the out-of-line buffer
  1783. * that we have defined for it.
  1784. */
  1785. STATIC int
  1786. xfs_attr_rmtval_set(xfs_da_args_t *args)
  1787. {
  1788. xfs_mount_t *mp;
  1789. xfs_fileoff_t lfileoff;
  1790. xfs_inode_t *dp;
  1791. xfs_bmbt_irec_t map;
  1792. xfs_daddr_t dblkno;
  1793. void *src;
  1794. xfs_buf_t *bp;
  1795. xfs_dablk_t lblkno;
  1796. int blkcnt, valuelen, nmap, error, tmp, committed;
  1797. dp = args->dp;
  1798. mp = dp->i_mount;
  1799. src = args->value;
  1800. /*
  1801. * Find a "hole" in the attribute address space large enough for
  1802. * us to drop the new attribute's value into.
  1803. */
  1804. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  1805. lfileoff = 0;
  1806. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  1807. XFS_ATTR_FORK);
  1808. if (error) {
  1809. return(error);
  1810. }
  1811. args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
  1812. args->rmtblkcnt = blkcnt;
  1813. /*
  1814. * Roll through the "value", allocating blocks on disk as required.
  1815. */
  1816. while (blkcnt > 0) {
  1817. /*
  1818. * Allocate a single extent, up to the size of the value.
  1819. */
  1820. xfs_bmap_init(args->flist, args->firstblock);
  1821. nmap = 1;
  1822. error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
  1823. blkcnt,
  1824. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1825. args->firstblock, args->total, &map, &nmap,
  1826. args->flist);
  1827. if (!error) {
  1828. error = xfs_bmap_finish(&args->trans, args->flist,
  1829. &committed);
  1830. }
  1831. if (error) {
  1832. ASSERT(committed);
  1833. args->trans = NULL;
  1834. xfs_bmap_cancel(args->flist);
  1835. return(error);
  1836. }
  1837. /*
  1838. * bmap_finish() may have committed the last trans and started
  1839. * a new one. We need the inode to be in all transactions.
  1840. */
  1841. if (committed)
  1842. xfs_trans_ijoin(args->trans, dp, 0);
  1843. ASSERT(nmap == 1);
  1844. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1845. (map.br_startblock != HOLESTARTBLOCK));
  1846. lblkno += map.br_blockcount;
  1847. blkcnt -= map.br_blockcount;
  1848. /*
  1849. * Start the next trans in the chain.
  1850. */
  1851. error = xfs_trans_roll(&args->trans, dp);
  1852. if (error)
  1853. return (error);
  1854. }
  1855. /*
  1856. * Roll through the "value", copying the attribute value to the
  1857. * already-allocated blocks. Blocks are written synchronously
  1858. * so that we can know they are all on disk before we turn off
  1859. * the INCOMPLETE flag.
  1860. */
  1861. lblkno = args->rmtblkno;
  1862. valuelen = args->valuelen;
  1863. while (valuelen > 0) {
  1864. int buflen;
  1865. /*
  1866. * Try to remember where we decided to put the value.
  1867. */
  1868. xfs_bmap_init(args->flist, args->firstblock);
  1869. nmap = 1;
  1870. error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
  1871. args->rmtblkcnt, &map, &nmap,
  1872. XFS_BMAPI_ATTRFORK);
  1873. if (error)
  1874. return(error);
  1875. ASSERT(nmap == 1);
  1876. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1877. (map.br_startblock != HOLESTARTBLOCK));
  1878. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1879. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1880. bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
  1881. if (!bp)
  1882. return ENOMEM;
  1883. buflen = BBTOB(bp->b_length);
  1884. tmp = min_t(int, valuelen, buflen);
  1885. xfs_buf_iomove(bp, 0, tmp, src, XBRW_WRITE);
  1886. if (tmp < buflen)
  1887. xfs_buf_zero(bp, tmp, buflen - tmp);
  1888. error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
  1889. xfs_buf_relse(bp);
  1890. if (error)
  1891. return error;
  1892. src += tmp;
  1893. valuelen -= tmp;
  1894. lblkno += map.br_blockcount;
  1895. }
  1896. ASSERT(valuelen == 0);
  1897. return(0);
  1898. }
  1899. /*
  1900. * Remove the value associated with an attribute by deleting the
  1901. * out-of-line buffer that it is stored on.
  1902. */
  1903. STATIC int
  1904. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  1905. {
  1906. xfs_mount_t *mp;
  1907. xfs_bmbt_irec_t map;
  1908. xfs_buf_t *bp;
  1909. xfs_daddr_t dblkno;
  1910. xfs_dablk_t lblkno;
  1911. int valuelen, blkcnt, nmap, error, done, committed;
  1912. mp = args->dp->i_mount;
  1913. /*
  1914. * Roll through the "value", invalidating the attribute value's
  1915. * blocks.
  1916. */
  1917. lblkno = args->rmtblkno;
  1918. valuelen = args->rmtblkcnt;
  1919. while (valuelen > 0) {
  1920. /*
  1921. * Try to remember where we decided to put the value.
  1922. */
  1923. nmap = 1;
  1924. error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
  1925. args->rmtblkcnt, &map, &nmap,
  1926. XFS_BMAPI_ATTRFORK);
  1927. if (error)
  1928. return(error);
  1929. ASSERT(nmap == 1);
  1930. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1931. (map.br_startblock != HOLESTARTBLOCK));
  1932. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1933. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1934. /*
  1935. * If the "remote" value is in the cache, remove it.
  1936. */
  1937. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
  1938. if (bp) {
  1939. xfs_buf_stale(bp);
  1940. xfs_buf_relse(bp);
  1941. bp = NULL;
  1942. }
  1943. valuelen -= map.br_blockcount;
  1944. lblkno += map.br_blockcount;
  1945. }
  1946. /*
  1947. * Keep de-allocating extents until the remote-value region is gone.
  1948. */
  1949. lblkno = args->rmtblkno;
  1950. blkcnt = args->rmtblkcnt;
  1951. done = 0;
  1952. while (!done) {
  1953. xfs_bmap_init(args->flist, args->firstblock);
  1954. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  1955. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1956. 1, args->firstblock, args->flist,
  1957. &done);
  1958. if (!error) {
  1959. error = xfs_bmap_finish(&args->trans, args->flist,
  1960. &committed);
  1961. }
  1962. if (error) {
  1963. ASSERT(committed);
  1964. args->trans = NULL;
  1965. xfs_bmap_cancel(args->flist);
  1966. return(error);
  1967. }
  1968. /*
  1969. * bmap_finish() may have committed the last trans and started
  1970. * a new one. We need the inode to be in all transactions.
  1971. */
  1972. if (committed)
  1973. xfs_trans_ijoin(args->trans, args->dp, 0);
  1974. /*
  1975. * Close out trans and start the next one in the chain.
  1976. */
  1977. error = xfs_trans_roll(&args->trans, args->dp);
  1978. if (error)
  1979. return (error);
  1980. }
  1981. return(0);
  1982. }