xfs_attr.c 54 KB

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