xfs_attr.c 59 KB

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