xfs_attr.c 54 KB

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