xfs_attr_item.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Oracle. All Rights Reserved.
  4. * Author: Allison Henderson <allison.henderson@oracle.com>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_trans_resv.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_defer.h"
  13. #include "xfs_log_format.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_bmap_btree.h"
  16. #include "xfs_trans_priv.h"
  17. #include "xfs_log.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_da_format.h"
  20. #include "xfs_da_btree.h"
  21. #include "xfs_attr.h"
  22. #include "xfs_attr_item.h"
  23. #include "xfs_trace.h"
  24. #include "xfs_trans_space.h"
  25. #include "xfs_errortag.h"
  26. #include "xfs_error.h"
  27. #include "xfs_log_priv.h"
  28. #include "xfs_log_recover.h"
  29. #include "xfs_parent.h"
  30. struct kmem_cache *xfs_attri_cache;
  31. struct kmem_cache *xfs_attrd_cache;
  32. static const struct xfs_item_ops xfs_attri_item_ops;
  33. static const struct xfs_item_ops xfs_attrd_item_ops;
  34. static inline struct xfs_attri_log_item *ATTRI_ITEM(struct xfs_log_item *lip)
  35. {
  36. return container_of(lip, struct xfs_attri_log_item, attri_item);
  37. }
  38. /*
  39. * Shared xattr name/value buffers for logged extended attribute operations
  40. *
  41. * When logging updates to extended attributes, we can create quite a few
  42. * attribute log intent items for a single xattr update. To avoid cycling the
  43. * memory allocator and memcpy overhead, the name (and value, for setxattr)
  44. * are kept in a refcounted object that is shared across all related log items
  45. * and the upper-level deferred work state structure. The shared buffer has
  46. * a control structure, followed by the name, and then the value.
  47. */
  48. static inline struct xfs_attri_log_nameval *
  49. xfs_attri_log_nameval_get(
  50. struct xfs_attri_log_nameval *nv)
  51. {
  52. if (!refcount_inc_not_zero(&nv->refcount))
  53. return NULL;
  54. return nv;
  55. }
  56. static inline void
  57. xfs_attri_log_nameval_put(
  58. struct xfs_attri_log_nameval *nv)
  59. {
  60. if (!nv)
  61. return;
  62. if (refcount_dec_and_test(&nv->refcount))
  63. kvfree(nv);
  64. }
  65. static inline struct xfs_attri_log_nameval *
  66. xfs_attri_log_nameval_alloc(
  67. const void *name,
  68. unsigned int name_len,
  69. const void *new_name,
  70. unsigned int new_name_len,
  71. const void *value,
  72. unsigned int value_len,
  73. const void *new_value,
  74. unsigned int new_value_len)
  75. {
  76. struct xfs_attri_log_nameval *nv;
  77. /*
  78. * This could be over 64kB in length, so we have to use kvmalloc() for
  79. * this. But kvmalloc() utterly sucks, so we use our own version.
  80. */
  81. nv = xlog_kvmalloc(sizeof(struct xfs_attri_log_nameval) +
  82. name_len + new_name_len + value_len +
  83. new_value_len);
  84. nv->name.i_addr = nv + 1;
  85. nv->name.i_len = name_len;
  86. nv->name.i_type = XLOG_REG_TYPE_ATTR_NAME;
  87. memcpy(nv->name.i_addr, name, name_len);
  88. if (new_name_len) {
  89. nv->new_name.i_addr = nv->name.i_addr + name_len;
  90. nv->new_name.i_len = new_name_len;
  91. memcpy(nv->new_name.i_addr, new_name, new_name_len);
  92. } else {
  93. nv->new_name.i_addr = NULL;
  94. nv->new_name.i_len = 0;
  95. }
  96. nv->new_name.i_type = XLOG_REG_TYPE_ATTR_NEWNAME;
  97. if (value_len) {
  98. nv->value.i_addr = nv->name.i_addr + name_len + new_name_len;
  99. nv->value.i_len = value_len;
  100. memcpy(nv->value.i_addr, value, value_len);
  101. } else {
  102. nv->value.i_addr = NULL;
  103. nv->value.i_len = 0;
  104. }
  105. nv->value.i_type = XLOG_REG_TYPE_ATTR_VALUE;
  106. if (new_value_len) {
  107. nv->new_value.i_addr = nv->name.i_addr + name_len +
  108. new_name_len + value_len;
  109. nv->new_value.i_len = new_value_len;
  110. memcpy(nv->new_value.i_addr, new_value, new_value_len);
  111. } else {
  112. nv->new_value.i_addr = NULL;
  113. nv->new_value.i_len = 0;
  114. }
  115. nv->new_value.i_type = XLOG_REG_TYPE_ATTR_NEWVALUE;
  116. refcount_set(&nv->refcount, 1);
  117. return nv;
  118. }
  119. STATIC void
  120. xfs_attri_item_free(
  121. struct xfs_attri_log_item *attrip)
  122. {
  123. kvfree(attrip->attri_item.li_lv_shadow);
  124. xfs_attri_log_nameval_put(attrip->attri_nameval);
  125. kmem_cache_free(xfs_attri_cache, attrip);
  126. }
  127. /*
  128. * Freeing the attrip requires that we remove it from the AIL if it has already
  129. * been placed there. However, the ATTRI may not yet have been placed in the
  130. * AIL when called by xfs_attri_release() from ATTRD processing due to the
  131. * ordering of committed vs unpin operations in bulk insert operations. Hence
  132. * the reference count to ensure only the last caller frees the ATTRI.
  133. */
  134. STATIC void
  135. xfs_attri_release(
  136. struct xfs_attri_log_item *attrip)
  137. {
  138. ASSERT(atomic_read(&attrip->attri_refcount) > 0);
  139. if (!atomic_dec_and_test(&attrip->attri_refcount))
  140. return;
  141. xfs_trans_ail_delete(&attrip->attri_item, 0);
  142. xfs_attri_item_free(attrip);
  143. }
  144. STATIC void
  145. xfs_attri_item_size(
  146. struct xfs_log_item *lip,
  147. int *nvecs,
  148. int *nbytes)
  149. {
  150. struct xfs_attri_log_item *attrip = ATTRI_ITEM(lip);
  151. struct xfs_attri_log_nameval *nv = attrip->attri_nameval;
  152. *nvecs += 2;
  153. *nbytes += sizeof(struct xfs_attri_log_format) +
  154. xlog_calc_iovec_len(nv->name.i_len);
  155. if (nv->new_name.i_len) {
  156. *nvecs += 1;
  157. *nbytes += xlog_calc_iovec_len(nv->new_name.i_len);
  158. }
  159. if (nv->value.i_len) {
  160. *nvecs += 1;
  161. *nbytes += xlog_calc_iovec_len(nv->value.i_len);
  162. }
  163. if (nv->new_value.i_len) {
  164. *nvecs += 1;
  165. *nbytes += xlog_calc_iovec_len(nv->new_value.i_len);
  166. }
  167. }
  168. /*
  169. * This is called to fill in the log iovecs for the given attri log
  170. * item. We use 1 iovec for the attri_format_item, 1 for the name, and
  171. * another for the value if it is present
  172. */
  173. STATIC void
  174. xfs_attri_item_format(
  175. struct xfs_log_item *lip,
  176. struct xfs_log_vec *lv)
  177. {
  178. struct xfs_attri_log_item *attrip = ATTRI_ITEM(lip);
  179. struct xfs_log_iovec *vecp = NULL;
  180. struct xfs_attri_log_nameval *nv = attrip->attri_nameval;
  181. attrip->attri_format.alfi_type = XFS_LI_ATTRI;
  182. attrip->attri_format.alfi_size = 1;
  183. /*
  184. * This size accounting must be done before copying the attrip into the
  185. * iovec. If we do it after, the wrong size will be recorded to the log
  186. * and we trip across assertion checks for bad region sizes later during
  187. * the log recovery.
  188. */
  189. ASSERT(nv->name.i_len > 0);
  190. attrip->attri_format.alfi_size++;
  191. if (nv->new_name.i_len > 0)
  192. attrip->attri_format.alfi_size++;
  193. if (nv->value.i_len > 0)
  194. attrip->attri_format.alfi_size++;
  195. if (nv->new_value.i_len > 0)
  196. attrip->attri_format.alfi_size++;
  197. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRI_FORMAT,
  198. &attrip->attri_format,
  199. sizeof(struct xfs_attri_log_format));
  200. xlog_copy_from_iovec(lv, &vecp, &nv->name);
  201. if (nv->new_name.i_len > 0)
  202. xlog_copy_from_iovec(lv, &vecp, &nv->new_name);
  203. if (nv->value.i_len > 0)
  204. xlog_copy_from_iovec(lv, &vecp, &nv->value);
  205. if (nv->new_value.i_len > 0)
  206. xlog_copy_from_iovec(lv, &vecp, &nv->new_value);
  207. }
  208. /*
  209. * The unpin operation is the last place an ATTRI is manipulated in the log. It
  210. * is either inserted in the AIL or aborted in the event of a log I/O error. In
  211. * either case, the ATTRI transaction has been successfully committed to make
  212. * it this far. Therefore, we expect whoever committed the ATTRI to either
  213. * construct and commit the ATTRD or drop the ATTRD's reference in the event of
  214. * error. Simply drop the log's ATTRI reference now that the log is done with
  215. * it.
  216. */
  217. STATIC void
  218. xfs_attri_item_unpin(
  219. struct xfs_log_item *lip,
  220. int remove)
  221. {
  222. xfs_attri_release(ATTRI_ITEM(lip));
  223. }
  224. STATIC void
  225. xfs_attri_item_release(
  226. struct xfs_log_item *lip)
  227. {
  228. xfs_attri_release(ATTRI_ITEM(lip));
  229. }
  230. /*
  231. * Allocate and initialize an attri item. Caller may allocate an additional
  232. * trailing buffer for name and value
  233. */
  234. STATIC struct xfs_attri_log_item *
  235. xfs_attri_init(
  236. struct xfs_mount *mp,
  237. struct xfs_attri_log_nameval *nv)
  238. {
  239. struct xfs_attri_log_item *attrip;
  240. attrip = kmem_cache_zalloc(xfs_attri_cache, GFP_KERNEL | __GFP_NOFAIL);
  241. /*
  242. * Grab an extra reference to the name/value buffer for this log item.
  243. * The caller retains its own reference!
  244. */
  245. attrip->attri_nameval = xfs_attri_log_nameval_get(nv);
  246. ASSERT(attrip->attri_nameval);
  247. xfs_log_item_init(mp, &attrip->attri_item, XFS_LI_ATTRI,
  248. &xfs_attri_item_ops);
  249. attrip->attri_format.alfi_id = (uintptr_t)(void *)attrip;
  250. atomic_set(&attrip->attri_refcount, 2);
  251. return attrip;
  252. }
  253. static inline struct xfs_attrd_log_item *ATTRD_ITEM(struct xfs_log_item *lip)
  254. {
  255. return container_of(lip, struct xfs_attrd_log_item, attrd_item);
  256. }
  257. STATIC void
  258. xfs_attrd_item_free(struct xfs_attrd_log_item *attrdp)
  259. {
  260. kvfree(attrdp->attrd_item.li_lv_shadow);
  261. kmem_cache_free(xfs_attrd_cache, attrdp);
  262. }
  263. STATIC void
  264. xfs_attrd_item_size(
  265. struct xfs_log_item *lip,
  266. int *nvecs,
  267. int *nbytes)
  268. {
  269. *nvecs += 1;
  270. *nbytes += sizeof(struct xfs_attrd_log_format);
  271. }
  272. /*
  273. * This is called to fill in the log iovecs for the given attrd log item. We use
  274. * only 1 iovec for the attrd_format, and we point that at the attr_log_format
  275. * structure embedded in the attrd item.
  276. */
  277. STATIC void
  278. xfs_attrd_item_format(
  279. struct xfs_log_item *lip,
  280. struct xfs_log_vec *lv)
  281. {
  282. struct xfs_attrd_log_item *attrdp = ATTRD_ITEM(lip);
  283. struct xfs_log_iovec *vecp = NULL;
  284. attrdp->attrd_format.alfd_type = XFS_LI_ATTRD;
  285. attrdp->attrd_format.alfd_size = 1;
  286. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ATTRD_FORMAT,
  287. &attrdp->attrd_format,
  288. sizeof(struct xfs_attrd_log_format));
  289. }
  290. /*
  291. * The ATTRD is either committed or aborted if the transaction is canceled. If
  292. * the transaction is canceled, drop our reference to the ATTRI and free the
  293. * ATTRD.
  294. */
  295. STATIC void
  296. xfs_attrd_item_release(
  297. struct xfs_log_item *lip)
  298. {
  299. struct xfs_attrd_log_item *attrdp = ATTRD_ITEM(lip);
  300. xfs_attri_release(attrdp->attrd_attrip);
  301. xfs_attrd_item_free(attrdp);
  302. }
  303. static struct xfs_log_item *
  304. xfs_attrd_item_intent(
  305. struct xfs_log_item *lip)
  306. {
  307. return &ATTRD_ITEM(lip)->attrd_attrip->attri_item;
  308. }
  309. static inline unsigned int
  310. xfs_attr_log_item_op(const struct xfs_attri_log_format *attrp)
  311. {
  312. return attrp->alfi_op_flags & XFS_ATTRI_OP_FLAGS_TYPE_MASK;
  313. }
  314. /* Log an attr to the intent item. */
  315. STATIC void
  316. xfs_attr_log_item(
  317. struct xfs_trans *tp,
  318. struct xfs_attri_log_item *attrip,
  319. const struct xfs_attr_intent *attr)
  320. {
  321. struct xfs_attri_log_format *attrp;
  322. struct xfs_attri_log_nameval *nv = attr->xattri_nameval;
  323. struct xfs_da_args *args = attr->xattri_da_args;
  324. /*
  325. * At this point the xfs_attr_intent has been constructed, and we've
  326. * created the log intent. Fill in the attri log item and log format
  327. * structure with fields from this xfs_attr_intent
  328. */
  329. attrp = &attrip->attri_format;
  330. attrp->alfi_ino = args->dp->i_ino;
  331. ASSERT(!(attr->xattri_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK));
  332. attrp->alfi_op_flags = attr->xattri_op_flags;
  333. attrp->alfi_value_len = nv->value.i_len;
  334. switch (xfs_attr_log_item_op(attrp)) {
  335. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  336. ASSERT(nv->value.i_len == nv->new_value.i_len);
  337. attrp->alfi_igen = VFS_I(args->dp)->i_generation;
  338. attrp->alfi_old_name_len = nv->name.i_len;
  339. attrp->alfi_new_name_len = nv->new_name.i_len;
  340. break;
  341. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  342. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  343. attrp->alfi_igen = VFS_I(args->dp)->i_generation;
  344. fallthrough;
  345. default:
  346. attrp->alfi_name_len = nv->name.i_len;
  347. break;
  348. }
  349. ASSERT(!(args->attr_filter & ~XFS_ATTRI_FILTER_MASK));
  350. attrp->alfi_attr_filter = args->attr_filter;
  351. }
  352. /* Get an ATTRI. */
  353. static struct xfs_log_item *
  354. xfs_attr_create_intent(
  355. struct xfs_trans *tp,
  356. struct list_head *items,
  357. unsigned int count,
  358. bool sort)
  359. {
  360. struct xfs_mount *mp = tp->t_mountp;
  361. struct xfs_attri_log_item *attrip;
  362. struct xfs_attr_intent *attr;
  363. struct xfs_da_args *args;
  364. ASSERT(count == 1);
  365. /*
  366. * Each attr item only performs one attribute operation at a time, so
  367. * this is a list of one
  368. */
  369. attr = list_first_entry_or_null(items, struct xfs_attr_intent,
  370. xattri_list);
  371. args = attr->xattri_da_args;
  372. if (!(args->op_flags & XFS_DA_OP_LOGGED))
  373. return NULL;
  374. /*
  375. * Create a buffer to store the attribute name and value. This buffer
  376. * will be shared between the higher level deferred xattr work state
  377. * and the lower level xattr log items.
  378. */
  379. if (!attr->xattri_nameval) {
  380. /*
  381. * Transfer our reference to the name/value buffer to the
  382. * deferred work state structure.
  383. */
  384. attr->xattri_nameval = xfs_attri_log_nameval_alloc(
  385. args->name, args->namelen,
  386. args->new_name, args->new_namelen,
  387. args->value, args->valuelen,
  388. args->new_value, args->new_valuelen);
  389. }
  390. attrip = xfs_attri_init(mp, attr->xattri_nameval);
  391. xfs_attr_log_item(tp, attrip, attr);
  392. return &attrip->attri_item;
  393. }
  394. static inline void
  395. xfs_attr_free_item(
  396. struct xfs_attr_intent *attr)
  397. {
  398. if (attr->xattri_da_state)
  399. xfs_da_state_free(attr->xattri_da_state);
  400. xfs_attri_log_nameval_put(attr->xattri_nameval);
  401. if (attr->xattri_da_args->op_flags & XFS_DA_OP_RECOVERY)
  402. kfree(attr);
  403. else
  404. kmem_cache_free(xfs_attr_intent_cache, attr);
  405. }
  406. static inline struct xfs_attr_intent *attri_entry(const struct list_head *e)
  407. {
  408. return list_entry(e, struct xfs_attr_intent, xattri_list);
  409. }
  410. /* Process an attr. */
  411. STATIC int
  412. xfs_attr_finish_item(
  413. struct xfs_trans *tp,
  414. struct xfs_log_item *done,
  415. struct list_head *item,
  416. struct xfs_btree_cur **state)
  417. {
  418. struct xfs_attr_intent *attr = attri_entry(item);
  419. struct xfs_da_args *args;
  420. int error;
  421. args = attr->xattri_da_args;
  422. /* Reset trans after EAGAIN cycle since the transaction is new */
  423. args->trans = tp;
  424. if (XFS_TEST_ERROR(false, args->dp->i_mount, XFS_ERRTAG_LARP)) {
  425. error = -EIO;
  426. goto out;
  427. }
  428. /* If an attr removal is trivially complete, we're done. */
  429. if (attr->xattri_op_flags == XFS_ATTRI_OP_FLAGS_REMOVE &&
  430. !xfs_inode_hasattr(args->dp)) {
  431. error = 0;
  432. goto out;
  433. }
  434. error = xfs_attr_set_iter(attr);
  435. if (!error && attr->xattri_dela_state != XFS_DAS_DONE)
  436. return -EAGAIN;
  437. out:
  438. xfs_attr_free_item(attr);
  439. return error;
  440. }
  441. /* Abort all pending ATTRs. */
  442. STATIC void
  443. xfs_attr_abort_intent(
  444. struct xfs_log_item *intent)
  445. {
  446. xfs_attri_release(ATTRI_ITEM(intent));
  447. }
  448. /* Cancel an attr */
  449. STATIC void
  450. xfs_attr_cancel_item(
  451. struct list_head *item)
  452. {
  453. struct xfs_attr_intent *attr = attri_entry(item);
  454. xfs_attr_free_item(attr);
  455. }
  456. STATIC bool
  457. xfs_attri_item_match(
  458. struct xfs_log_item *lip,
  459. uint64_t intent_id)
  460. {
  461. return ATTRI_ITEM(lip)->attri_format.alfi_id == intent_id;
  462. }
  463. static inline bool
  464. xfs_attri_validate_namelen(unsigned int namelen)
  465. {
  466. return namelen > 0 && namelen <= XATTR_NAME_MAX;
  467. }
  468. /* Is this recovered ATTRI format ok? */
  469. static inline bool
  470. xfs_attri_validate(
  471. struct xfs_mount *mp,
  472. struct xfs_attri_log_format *attrp)
  473. {
  474. unsigned int op = xfs_attr_log_item_op(attrp);
  475. if (attrp->alfi_op_flags & ~XFS_ATTRI_OP_FLAGS_TYPE_MASK)
  476. return false;
  477. if (attrp->alfi_attr_filter & ~XFS_ATTRI_FILTER_MASK)
  478. return false;
  479. if (!xfs_attr_check_namespace(attrp->alfi_attr_filter &
  480. XFS_ATTR_NSP_ONDISK_MASK))
  481. return false;
  482. switch (op) {
  483. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  484. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  485. if (!xfs_has_parent(mp))
  486. return false;
  487. if (attrp->alfi_value_len != sizeof(struct xfs_parent_rec))
  488. return false;
  489. if (!xfs_attri_validate_namelen(attrp->alfi_name_len))
  490. return false;
  491. if (!(attrp->alfi_attr_filter & XFS_ATTR_PARENT))
  492. return false;
  493. break;
  494. case XFS_ATTRI_OP_FLAGS_SET:
  495. case XFS_ATTRI_OP_FLAGS_REPLACE:
  496. if (!xfs_is_using_logged_xattrs(mp))
  497. return false;
  498. if (attrp->alfi_value_len > XATTR_SIZE_MAX)
  499. return false;
  500. if (!xfs_attri_validate_namelen(attrp->alfi_name_len))
  501. return false;
  502. break;
  503. case XFS_ATTRI_OP_FLAGS_REMOVE:
  504. if (!xfs_is_using_logged_xattrs(mp))
  505. return false;
  506. if (attrp->alfi_value_len != 0)
  507. return false;
  508. if (!xfs_attri_validate_namelen(attrp->alfi_name_len))
  509. return false;
  510. break;
  511. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  512. if (!xfs_has_parent(mp))
  513. return false;
  514. if (!xfs_attri_validate_namelen(attrp->alfi_old_name_len))
  515. return false;
  516. if (!xfs_attri_validate_namelen(attrp->alfi_new_name_len))
  517. return false;
  518. if (attrp->alfi_value_len != sizeof(struct xfs_parent_rec))
  519. return false;
  520. if (!(attrp->alfi_attr_filter & XFS_ATTR_PARENT))
  521. return false;
  522. break;
  523. default:
  524. return false;
  525. }
  526. return xfs_verify_ino(mp, attrp->alfi_ino);
  527. }
  528. static int
  529. xfs_attri_iread_extents(
  530. struct xfs_inode *ip)
  531. {
  532. struct xfs_trans *tp;
  533. int error;
  534. error = xfs_trans_alloc_empty(ip->i_mount, &tp);
  535. if (error)
  536. return error;
  537. xfs_ilock(ip, XFS_ILOCK_EXCL);
  538. error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
  539. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  540. xfs_trans_cancel(tp);
  541. return error;
  542. }
  543. static inline struct xfs_attr_intent *
  544. xfs_attri_recover_work(
  545. struct xfs_mount *mp,
  546. struct xfs_defer_pending *dfp,
  547. struct xfs_attri_log_format *attrp,
  548. struct xfs_inode **ipp,
  549. struct xfs_attri_log_nameval *nv)
  550. {
  551. struct xfs_attr_intent *attr;
  552. struct xfs_da_args *args;
  553. struct xfs_inode *ip;
  554. int local;
  555. int error;
  556. /*
  557. * Parent pointer attr items record the generation but regular logged
  558. * xattrs do not; select the right iget function.
  559. */
  560. switch (xfs_attr_log_item_op(attrp)) {
  561. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  562. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  563. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  564. error = xlog_recover_iget_handle(mp, attrp->alfi_ino,
  565. attrp->alfi_igen, &ip);
  566. break;
  567. default:
  568. error = xlog_recover_iget(mp, attrp->alfi_ino, &ip);
  569. break;
  570. }
  571. if (error) {
  572. xfs_irele(ip);
  573. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, attrp,
  574. sizeof(*attrp));
  575. return ERR_PTR(-EFSCORRUPTED);
  576. }
  577. if (xfs_inode_has_attr_fork(ip)) {
  578. error = xfs_attri_iread_extents(ip);
  579. if (error) {
  580. xfs_irele(ip);
  581. return ERR_PTR(error);
  582. }
  583. }
  584. attr = kzalloc(sizeof(struct xfs_attr_intent) +
  585. sizeof(struct xfs_da_args), GFP_KERNEL | __GFP_NOFAIL);
  586. args = (struct xfs_da_args *)(attr + 1);
  587. attr->xattri_da_args = args;
  588. attr->xattri_op_flags = xfs_attr_log_item_op(attrp);
  589. /*
  590. * We're reconstructing the deferred work state structure from the
  591. * recovered log item. Grab a reference to the name/value buffer and
  592. * attach it to the new work state.
  593. */
  594. attr->xattri_nameval = xfs_attri_log_nameval_get(nv);
  595. ASSERT(attr->xattri_nameval);
  596. args->dp = ip;
  597. args->geo = mp->m_attr_geo;
  598. args->whichfork = XFS_ATTR_FORK;
  599. args->name = nv->name.i_addr;
  600. args->namelen = nv->name.i_len;
  601. args->new_name = nv->new_name.i_addr;
  602. args->new_namelen = nv->new_name.i_len;
  603. args->value = nv->value.i_addr;
  604. args->valuelen = nv->value.i_len;
  605. args->new_value = nv->new_value.i_addr;
  606. args->new_valuelen = nv->new_value.i_len;
  607. args->attr_filter = attrp->alfi_attr_filter & XFS_ATTRI_FILTER_MASK;
  608. args->op_flags = XFS_DA_OP_RECOVERY | XFS_DA_OP_OKNOENT |
  609. XFS_DA_OP_LOGGED;
  610. args->owner = args->dp->i_ino;
  611. xfs_attr_sethash(args);
  612. switch (xfs_attr_intent_op(attr)) {
  613. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  614. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  615. case XFS_ATTRI_OP_FLAGS_SET:
  616. case XFS_ATTRI_OP_FLAGS_REPLACE:
  617. args->total = xfs_attr_calc_size(args, &local);
  618. if (xfs_inode_hasattr(args->dp))
  619. attr->xattri_dela_state = xfs_attr_init_replace_state(args);
  620. else
  621. attr->xattri_dela_state = xfs_attr_init_add_state(args);
  622. break;
  623. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  624. case XFS_ATTRI_OP_FLAGS_REMOVE:
  625. attr->xattri_dela_state = xfs_attr_init_remove_state(args);
  626. break;
  627. }
  628. xfs_defer_add_item(dfp, &attr->xattri_list);
  629. *ipp = ip;
  630. return attr;
  631. }
  632. /*
  633. * Process an attr intent item that was recovered from the log. We need to
  634. * delete the attr that it describes.
  635. */
  636. STATIC int
  637. xfs_attr_recover_work(
  638. struct xfs_defer_pending *dfp,
  639. struct list_head *capture_list)
  640. {
  641. struct xfs_log_item *lip = dfp->dfp_intent;
  642. struct xfs_attri_log_item *attrip = ATTRI_ITEM(lip);
  643. struct xfs_attr_intent *attr;
  644. struct xfs_mount *mp = lip->li_log->l_mp;
  645. struct xfs_inode *ip;
  646. struct xfs_da_args *args;
  647. struct xfs_trans *tp;
  648. struct xfs_trans_res resv;
  649. struct xfs_attri_log_format *attrp;
  650. struct xfs_attri_log_nameval *nv = attrip->attri_nameval;
  651. int error;
  652. unsigned int total = 0;
  653. /*
  654. * First check the validity of the attr described by the ATTRI. If any
  655. * are bad, then assume that all are bad and just toss the ATTRI.
  656. */
  657. attrp = &attrip->attri_format;
  658. if (!xfs_attri_validate(mp, attrp) ||
  659. !xfs_attr_namecheck(attrp->alfi_attr_filter, nv->name.i_addr,
  660. nv->name.i_len))
  661. return -EFSCORRUPTED;
  662. attr = xfs_attri_recover_work(mp, dfp, attrp, &ip, nv);
  663. if (IS_ERR(attr))
  664. return PTR_ERR(attr);
  665. args = attr->xattri_da_args;
  666. switch (xfs_attr_intent_op(attr)) {
  667. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  668. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  669. case XFS_ATTRI_OP_FLAGS_SET:
  670. case XFS_ATTRI_OP_FLAGS_REPLACE:
  671. resv = xfs_attr_set_resv(args);
  672. total = args->total;
  673. break;
  674. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  675. case XFS_ATTRI_OP_FLAGS_REMOVE:
  676. resv = M_RES(mp)->tr_attrrm;
  677. total = XFS_ATTRRM_SPACE_RES(mp);
  678. break;
  679. }
  680. resv = xlog_recover_resv(&resv);
  681. error = xfs_trans_alloc(mp, &resv, total, 0, XFS_TRANS_RESERVE, &tp);
  682. if (error)
  683. return error;
  684. args->trans = tp;
  685. xfs_ilock(ip, XFS_ILOCK_EXCL);
  686. xfs_trans_ijoin(tp, ip, 0);
  687. error = xlog_recover_finish_intent(tp, dfp);
  688. if (error == -EFSCORRUPTED)
  689. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  690. &attrip->attri_format,
  691. sizeof(attrip->attri_format));
  692. if (error)
  693. goto out_cancel;
  694. error = xfs_defer_ops_capture_and_commit(tp, capture_list);
  695. out_unlock:
  696. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  697. xfs_irele(ip);
  698. return error;
  699. out_cancel:
  700. xfs_trans_cancel(tp);
  701. goto out_unlock;
  702. }
  703. /* Re-log an intent item to push the log tail forward. */
  704. static struct xfs_log_item *
  705. xfs_attr_relog_intent(
  706. struct xfs_trans *tp,
  707. struct xfs_log_item *intent,
  708. struct xfs_log_item *done_item)
  709. {
  710. struct xfs_attri_log_item *old_attrip;
  711. struct xfs_attri_log_item *new_attrip;
  712. struct xfs_attri_log_format *new_attrp;
  713. struct xfs_attri_log_format *old_attrp;
  714. old_attrip = ATTRI_ITEM(intent);
  715. old_attrp = &old_attrip->attri_format;
  716. /*
  717. * Create a new log item that shares the same name/value buffer as the
  718. * old log item.
  719. */
  720. new_attrip = xfs_attri_init(tp->t_mountp, old_attrip->attri_nameval);
  721. new_attrp = &new_attrip->attri_format;
  722. new_attrp->alfi_ino = old_attrp->alfi_ino;
  723. new_attrp->alfi_igen = old_attrp->alfi_igen;
  724. new_attrp->alfi_op_flags = old_attrp->alfi_op_flags;
  725. new_attrp->alfi_value_len = old_attrp->alfi_value_len;
  726. switch (xfs_attr_log_item_op(old_attrp)) {
  727. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  728. new_attrp->alfi_new_name_len = old_attrp->alfi_new_name_len;
  729. new_attrp->alfi_old_name_len = old_attrp->alfi_old_name_len;
  730. break;
  731. default:
  732. new_attrp->alfi_name_len = old_attrp->alfi_name_len;
  733. break;
  734. }
  735. new_attrp->alfi_attr_filter = old_attrp->alfi_attr_filter;
  736. return &new_attrip->attri_item;
  737. }
  738. /* Get an ATTRD so we can process all the attrs. */
  739. static struct xfs_log_item *
  740. xfs_attr_create_done(
  741. struct xfs_trans *tp,
  742. struct xfs_log_item *intent,
  743. unsigned int count)
  744. {
  745. struct xfs_attri_log_item *attrip;
  746. struct xfs_attrd_log_item *attrdp;
  747. attrip = ATTRI_ITEM(intent);
  748. attrdp = kmem_cache_zalloc(xfs_attrd_cache, GFP_KERNEL | __GFP_NOFAIL);
  749. xfs_log_item_init(tp->t_mountp, &attrdp->attrd_item, XFS_LI_ATTRD,
  750. &xfs_attrd_item_ops);
  751. attrdp->attrd_attrip = attrip;
  752. attrdp->attrd_format.alfd_alf_id = attrip->attri_format.alfi_id;
  753. return &attrdp->attrd_item;
  754. }
  755. void
  756. xfs_attr_defer_add(
  757. struct xfs_da_args *args,
  758. enum xfs_attr_defer_op op)
  759. {
  760. struct xfs_attr_intent *new;
  761. unsigned int log_op = 0;
  762. bool is_pptr = args->attr_filter & XFS_ATTR_PARENT;
  763. if (is_pptr) {
  764. ASSERT(xfs_has_parent(args->dp->i_mount));
  765. ASSERT((args->attr_filter & ~XFS_ATTR_PARENT) == 0);
  766. ASSERT(args->op_flags & XFS_DA_OP_LOGGED);
  767. ASSERT(args->valuelen == sizeof(struct xfs_parent_rec));
  768. }
  769. new = kmem_cache_zalloc(xfs_attr_intent_cache,
  770. GFP_NOFS | __GFP_NOFAIL);
  771. new->xattri_da_args = args;
  772. /* Compute log operation from the higher level op and namespace. */
  773. switch (op) {
  774. case XFS_ATTR_DEFER_SET:
  775. if (is_pptr)
  776. log_op = XFS_ATTRI_OP_FLAGS_PPTR_SET;
  777. else
  778. log_op = XFS_ATTRI_OP_FLAGS_SET;
  779. break;
  780. case XFS_ATTR_DEFER_REPLACE:
  781. if (is_pptr)
  782. log_op = XFS_ATTRI_OP_FLAGS_PPTR_REPLACE;
  783. else
  784. log_op = XFS_ATTRI_OP_FLAGS_REPLACE;
  785. break;
  786. case XFS_ATTR_DEFER_REMOVE:
  787. if (is_pptr)
  788. log_op = XFS_ATTRI_OP_FLAGS_PPTR_REMOVE;
  789. else
  790. log_op = XFS_ATTRI_OP_FLAGS_REMOVE;
  791. break;
  792. default:
  793. ASSERT(0);
  794. break;
  795. }
  796. new->xattri_op_flags = log_op;
  797. /* Set up initial attr operation state. */
  798. switch (log_op) {
  799. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  800. case XFS_ATTRI_OP_FLAGS_SET:
  801. new->xattri_dela_state = xfs_attr_init_add_state(args);
  802. break;
  803. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  804. ASSERT(args->new_valuelen == args->valuelen);
  805. new->xattri_dela_state = xfs_attr_init_replace_state(args);
  806. break;
  807. case XFS_ATTRI_OP_FLAGS_REPLACE:
  808. new->xattri_dela_state = xfs_attr_init_replace_state(args);
  809. break;
  810. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  811. case XFS_ATTRI_OP_FLAGS_REMOVE:
  812. new->xattri_dela_state = xfs_attr_init_remove_state(args);
  813. break;
  814. }
  815. xfs_defer_add(args->trans, &new->xattri_list, &xfs_attr_defer_type);
  816. trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp);
  817. }
  818. const struct xfs_defer_op_type xfs_attr_defer_type = {
  819. .name = "attr",
  820. .max_items = 1,
  821. .create_intent = xfs_attr_create_intent,
  822. .abort_intent = xfs_attr_abort_intent,
  823. .create_done = xfs_attr_create_done,
  824. .finish_item = xfs_attr_finish_item,
  825. .cancel_item = xfs_attr_cancel_item,
  826. .recover_work = xfs_attr_recover_work,
  827. .relog_intent = xfs_attr_relog_intent,
  828. };
  829. static inline void *
  830. xfs_attri_validate_name_iovec(
  831. struct xfs_mount *mp,
  832. struct xfs_attri_log_format *attri_formatp,
  833. const struct xfs_log_iovec *iovec,
  834. unsigned int name_len)
  835. {
  836. if (iovec->i_len != xlog_calc_iovec_len(name_len)) {
  837. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  838. attri_formatp, sizeof(*attri_formatp));
  839. return NULL;
  840. }
  841. if (!xfs_attr_namecheck(attri_formatp->alfi_attr_filter, iovec->i_addr,
  842. name_len)) {
  843. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  844. attri_formatp, sizeof(*attri_formatp));
  845. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  846. iovec->i_addr, iovec->i_len);
  847. return NULL;
  848. }
  849. return iovec->i_addr;
  850. }
  851. static inline void *
  852. xfs_attri_validate_value_iovec(
  853. struct xfs_mount *mp,
  854. struct xfs_attri_log_format *attri_formatp,
  855. const struct xfs_log_iovec *iovec,
  856. unsigned int value_len)
  857. {
  858. if (iovec->i_len != xlog_calc_iovec_len(value_len)) {
  859. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  860. attri_formatp, sizeof(*attri_formatp));
  861. return NULL;
  862. }
  863. if ((attri_formatp->alfi_attr_filter & XFS_ATTR_PARENT) &&
  864. !xfs_parent_valuecheck(mp, iovec->i_addr, value_len)) {
  865. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  866. attri_formatp, sizeof(*attri_formatp));
  867. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  868. iovec->i_addr, iovec->i_len);
  869. return NULL;
  870. }
  871. return iovec->i_addr;
  872. }
  873. STATIC int
  874. xlog_recover_attri_commit_pass2(
  875. struct xlog *log,
  876. struct list_head *buffer_list,
  877. struct xlog_recover_item *item,
  878. xfs_lsn_t lsn)
  879. {
  880. struct xfs_mount *mp = log->l_mp;
  881. struct xfs_attri_log_item *attrip;
  882. struct xfs_attri_log_format *attri_formatp;
  883. struct xfs_attri_log_nameval *nv;
  884. const void *attr_name;
  885. const void *attr_value = NULL;
  886. const void *attr_new_name = NULL;
  887. const void *attr_new_value = NULL;
  888. size_t len;
  889. unsigned int name_len = 0;
  890. unsigned int value_len = 0;
  891. unsigned int new_name_len = 0;
  892. unsigned int new_value_len = 0;
  893. unsigned int op, i = 0;
  894. /* Validate xfs_attri_log_format before the large memory allocation */
  895. len = sizeof(struct xfs_attri_log_format);
  896. if (item->ri_buf[i].i_len != len) {
  897. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  898. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  899. return -EFSCORRUPTED;
  900. }
  901. attri_formatp = item->ri_buf[i].i_addr;
  902. if (!xfs_attri_validate(mp, attri_formatp)) {
  903. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  904. attri_formatp, len);
  905. return -EFSCORRUPTED;
  906. }
  907. /* Check the number of log iovecs makes sense for the op code. */
  908. op = xfs_attr_log_item_op(attri_formatp);
  909. switch (op) {
  910. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  911. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  912. /* Log item, attr name, attr value */
  913. if (item->ri_total != 3) {
  914. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  915. attri_formatp, len);
  916. return -EFSCORRUPTED;
  917. }
  918. name_len = attri_formatp->alfi_name_len;
  919. value_len = attri_formatp->alfi_value_len;
  920. break;
  921. case XFS_ATTRI_OP_FLAGS_SET:
  922. case XFS_ATTRI_OP_FLAGS_REPLACE:
  923. /* Log item, attr name, attr value */
  924. if (item->ri_total != 3) {
  925. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  926. attri_formatp, len);
  927. return -EFSCORRUPTED;
  928. }
  929. name_len = attri_formatp->alfi_name_len;
  930. value_len = attri_formatp->alfi_value_len;
  931. break;
  932. case XFS_ATTRI_OP_FLAGS_REMOVE:
  933. /* Log item, attr name */
  934. if (item->ri_total != 2) {
  935. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  936. attri_formatp, len);
  937. return -EFSCORRUPTED;
  938. }
  939. name_len = attri_formatp->alfi_name_len;
  940. break;
  941. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  942. /*
  943. * Log item, attr name, new attr name, attr value, new attr
  944. * value
  945. */
  946. if (item->ri_total != 5) {
  947. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  948. attri_formatp, len);
  949. return -EFSCORRUPTED;
  950. }
  951. name_len = attri_formatp->alfi_old_name_len;
  952. new_name_len = attri_formatp->alfi_new_name_len;
  953. new_value_len = value_len = attri_formatp->alfi_value_len;
  954. break;
  955. default:
  956. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  957. attri_formatp, len);
  958. return -EFSCORRUPTED;
  959. }
  960. i++;
  961. /* Validate the attr name */
  962. attr_name = xfs_attri_validate_name_iovec(mp, attri_formatp,
  963. &item->ri_buf[i], name_len);
  964. if (!attr_name)
  965. return -EFSCORRUPTED;
  966. i++;
  967. /* Validate the new attr name */
  968. if (new_name_len > 0) {
  969. attr_new_name = xfs_attri_validate_name_iovec(mp,
  970. attri_formatp, &item->ri_buf[i],
  971. new_name_len);
  972. if (!attr_new_name)
  973. return -EFSCORRUPTED;
  974. i++;
  975. }
  976. /* Validate the attr value, if present */
  977. if (value_len != 0) {
  978. attr_value = xfs_attri_validate_value_iovec(mp, attri_formatp,
  979. &item->ri_buf[i], value_len);
  980. if (!attr_value)
  981. return -EFSCORRUPTED;
  982. i++;
  983. }
  984. /* Validate the new attr value, if present */
  985. if (new_value_len != 0) {
  986. attr_new_value = xfs_attri_validate_value_iovec(mp,
  987. attri_formatp, &item->ri_buf[i],
  988. new_value_len);
  989. if (!attr_new_value)
  990. return -EFSCORRUPTED;
  991. i++;
  992. }
  993. /*
  994. * Make sure we got the correct number of buffers for the operation
  995. * that we just loaded.
  996. */
  997. if (i != item->ri_total) {
  998. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  999. attri_formatp, len);
  1000. return -EFSCORRUPTED;
  1001. }
  1002. switch (op) {
  1003. case XFS_ATTRI_OP_FLAGS_REMOVE:
  1004. /* Regular remove operations operate only on names. */
  1005. if (attr_value != NULL || value_len != 0) {
  1006. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  1007. attri_formatp, len);
  1008. return -EFSCORRUPTED;
  1009. }
  1010. fallthrough;
  1011. case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
  1012. case XFS_ATTRI_OP_FLAGS_PPTR_SET:
  1013. case XFS_ATTRI_OP_FLAGS_SET:
  1014. case XFS_ATTRI_OP_FLAGS_REPLACE:
  1015. /*
  1016. * Regular xattr set/remove/replace operations require a name
  1017. * and do not take a newname. Values are optional for set and
  1018. * replace.
  1019. *
  1020. * Name-value set/remove operations must have a name, do not
  1021. * take a newname, and can take a value.
  1022. */
  1023. if (attr_name == NULL || name_len == 0) {
  1024. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  1025. attri_formatp, len);
  1026. return -EFSCORRUPTED;
  1027. }
  1028. break;
  1029. case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
  1030. /*
  1031. * Name-value replace operations require the caller to
  1032. * specify the old and new names and values explicitly.
  1033. * Values are optional.
  1034. */
  1035. if (attr_name == NULL || name_len == 0) {
  1036. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  1037. attri_formatp, len);
  1038. return -EFSCORRUPTED;
  1039. }
  1040. if (attr_new_name == NULL || new_name_len == 0) {
  1041. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
  1042. attri_formatp, len);
  1043. return -EFSCORRUPTED;
  1044. }
  1045. break;
  1046. }
  1047. /*
  1048. * Memory alloc failure will cause replay to abort. We attach the
  1049. * name/value buffer to the recovered incore log item and drop our
  1050. * reference.
  1051. */
  1052. nv = xfs_attri_log_nameval_alloc(attr_name, name_len,
  1053. attr_new_name, new_name_len,
  1054. attr_value, value_len,
  1055. attr_new_value, new_value_len);
  1056. attrip = xfs_attri_init(mp, nv);
  1057. memcpy(&attrip->attri_format, attri_formatp, len);
  1058. xlog_recover_intent_item(log, &attrip->attri_item, lsn,
  1059. &xfs_attr_defer_type);
  1060. xfs_attri_log_nameval_put(nv);
  1061. return 0;
  1062. }
  1063. /*
  1064. * This routine is called when an ATTRD format structure is found in a committed
  1065. * transaction in the log. Its purpose is to cancel the corresponding ATTRI if
  1066. * it was still in the log. To do this it searches the AIL for the ATTRI with
  1067. * an id equal to that in the ATTRD format structure. If we find it we drop
  1068. * the ATTRD reference, which removes the ATTRI from the AIL and frees it.
  1069. */
  1070. STATIC int
  1071. xlog_recover_attrd_commit_pass2(
  1072. struct xlog *log,
  1073. struct list_head *buffer_list,
  1074. struct xlog_recover_item *item,
  1075. xfs_lsn_t lsn)
  1076. {
  1077. struct xfs_attrd_log_format *attrd_formatp;
  1078. attrd_formatp = item->ri_buf[0].i_addr;
  1079. if (item->ri_buf[0].i_len != sizeof(struct xfs_attrd_log_format)) {
  1080. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
  1081. item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
  1082. return -EFSCORRUPTED;
  1083. }
  1084. xlog_recover_release_intent(log, XFS_LI_ATTRI,
  1085. attrd_formatp->alfd_alf_id);
  1086. return 0;
  1087. }
  1088. static const struct xfs_item_ops xfs_attri_item_ops = {
  1089. .flags = XFS_ITEM_INTENT,
  1090. .iop_size = xfs_attri_item_size,
  1091. .iop_format = xfs_attri_item_format,
  1092. .iop_unpin = xfs_attri_item_unpin,
  1093. .iop_release = xfs_attri_item_release,
  1094. .iop_match = xfs_attri_item_match,
  1095. };
  1096. const struct xlog_recover_item_ops xlog_attri_item_ops = {
  1097. .item_type = XFS_LI_ATTRI,
  1098. .commit_pass2 = xlog_recover_attri_commit_pass2,
  1099. };
  1100. static const struct xfs_item_ops xfs_attrd_item_ops = {
  1101. .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
  1102. XFS_ITEM_INTENT_DONE,
  1103. .iop_size = xfs_attrd_item_size,
  1104. .iop_format = xfs_attrd_item_format,
  1105. .iop_release = xfs_attrd_item_release,
  1106. .iop_intent = xfs_attrd_item_intent,
  1107. };
  1108. const struct xlog_recover_item_ops xlog_attrd_item_ops = {
  1109. .item_type = XFS_LI_ATTRD,
  1110. .commit_pass2 = xlog_recover_attrd_commit_pass2,
  1111. };