xfs_attr.c 56 KB

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