delayed-inode.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. /*
  2. * Copyright (C) 2011 Fujitsu. All rights reserved.
  3. * Written by Miao Xie <[email protected]>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 021110-1307, USA.
  18. */
  19. #include <linux/slab.h>
  20. #include "delayed-inode.h"
  21. #include "disk-io.h"
  22. #include "transaction.h"
  23. #include "ctree.h"
  24. #define BTRFS_DELAYED_WRITEBACK 512
  25. #define BTRFS_DELAYED_BACKGROUND 128
  26. #define BTRFS_DELAYED_BATCH 16
  27. static struct kmem_cache *delayed_node_cache;
  28. int __init btrfs_delayed_inode_init(void)
  29. {
  30. delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
  31. sizeof(struct btrfs_delayed_node),
  32. 0,
  33. SLAB_MEM_SPREAD,
  34. NULL);
  35. if (!delayed_node_cache)
  36. return -ENOMEM;
  37. return 0;
  38. }
  39. void btrfs_delayed_inode_exit(void)
  40. {
  41. kmem_cache_destroy(delayed_node_cache);
  42. }
  43. static inline void btrfs_init_delayed_node(
  44. struct btrfs_delayed_node *delayed_node,
  45. struct btrfs_root *root, u64 inode_id)
  46. {
  47. delayed_node->root = root;
  48. delayed_node->inode_id = inode_id;
  49. atomic_set(&delayed_node->refs, 0);
  50. delayed_node->ins_root = RB_ROOT;
  51. delayed_node->del_root = RB_ROOT;
  52. mutex_init(&delayed_node->mutex);
  53. INIT_LIST_HEAD(&delayed_node->n_list);
  54. INIT_LIST_HEAD(&delayed_node->p_list);
  55. }
  56. static inline int btrfs_is_continuous_delayed_item(
  57. struct btrfs_delayed_item *item1,
  58. struct btrfs_delayed_item *item2)
  59. {
  60. if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
  61. item1->key.objectid == item2->key.objectid &&
  62. item1->key.type == item2->key.type &&
  63. item1->key.offset + 1 == item2->key.offset)
  64. return 1;
  65. return 0;
  66. }
  67. static inline struct btrfs_delayed_root *btrfs_get_delayed_root(
  68. struct btrfs_root *root)
  69. {
  70. return root->fs_info->delayed_root;
  71. }
  72. static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
  73. {
  74. struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
  75. struct btrfs_root *root = btrfs_inode->root;
  76. u64 ino = btrfs_ino(inode);
  77. struct btrfs_delayed_node *node;
  78. node = ACCESS_ONCE(btrfs_inode->delayed_node);
  79. if (node) {
  80. atomic_inc(&node->refs);
  81. return node;
  82. }
  83. spin_lock(&root->inode_lock);
  84. node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
  85. if (node) {
  86. if (btrfs_inode->delayed_node) {
  87. atomic_inc(&node->refs); /* can be accessed */
  88. BUG_ON(btrfs_inode->delayed_node != node);
  89. spin_unlock(&root->inode_lock);
  90. return node;
  91. }
  92. btrfs_inode->delayed_node = node;
  93. /* can be accessed and cached in the inode */
  94. atomic_add(2, &node->refs);
  95. spin_unlock(&root->inode_lock);
  96. return node;
  97. }
  98. spin_unlock(&root->inode_lock);
  99. return NULL;
  100. }
  101. /* Will return either the node or PTR_ERR(-ENOMEM) */
  102. static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
  103. struct inode *inode)
  104. {
  105. struct btrfs_delayed_node *node;
  106. struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
  107. struct btrfs_root *root = btrfs_inode->root;
  108. u64 ino = btrfs_ino(inode);
  109. int ret;
  110. again:
  111. node = btrfs_get_delayed_node(inode);
  112. if (node)
  113. return node;
  114. node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
  115. if (!node)
  116. return ERR_PTR(-ENOMEM);
  117. btrfs_init_delayed_node(node, root, ino);
  118. /* cached in the btrfs inode and can be accessed */
  119. atomic_add(2, &node->refs);
  120. ret = radix_tree_preload(GFP_NOFS);
  121. if (ret) {
  122. kmem_cache_free(delayed_node_cache, node);
  123. return ERR_PTR(ret);
  124. }
  125. spin_lock(&root->inode_lock);
  126. ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
  127. if (ret == -EEXIST) {
  128. spin_unlock(&root->inode_lock);
  129. kmem_cache_free(delayed_node_cache, node);
  130. radix_tree_preload_end();
  131. goto again;
  132. }
  133. btrfs_inode->delayed_node = node;
  134. spin_unlock(&root->inode_lock);
  135. radix_tree_preload_end();
  136. return node;
  137. }
  138. /*
  139. * Call it when holding delayed_node->mutex
  140. *
  141. * If mod = 1, add this node into the prepared list.
  142. */
  143. static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
  144. struct btrfs_delayed_node *node,
  145. int mod)
  146. {
  147. spin_lock(&root->lock);
  148. if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
  149. if (!list_empty(&node->p_list))
  150. list_move_tail(&node->p_list, &root->prepare_list);
  151. else if (mod)
  152. list_add_tail(&node->p_list, &root->prepare_list);
  153. } else {
  154. list_add_tail(&node->n_list, &root->node_list);
  155. list_add_tail(&node->p_list, &root->prepare_list);
  156. atomic_inc(&node->refs); /* inserted into list */
  157. root->nodes++;
  158. set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
  159. }
  160. spin_unlock(&root->lock);
  161. }
  162. /* Call it when holding delayed_node->mutex */
  163. static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
  164. struct btrfs_delayed_node *node)
  165. {
  166. spin_lock(&root->lock);
  167. if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
  168. root->nodes--;
  169. atomic_dec(&node->refs); /* not in the list */
  170. list_del_init(&node->n_list);
  171. if (!list_empty(&node->p_list))
  172. list_del_init(&node->p_list);
  173. clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
  174. }
  175. spin_unlock(&root->lock);
  176. }
  177. static struct btrfs_delayed_node *btrfs_first_delayed_node(
  178. struct btrfs_delayed_root *delayed_root)
  179. {
  180. struct list_head *p;
  181. struct btrfs_delayed_node *node = NULL;
  182. spin_lock(&delayed_root->lock);
  183. if (list_empty(&delayed_root->node_list))
  184. goto out;
  185. p = delayed_root->node_list.next;
  186. node = list_entry(p, struct btrfs_delayed_node, n_list);
  187. atomic_inc(&node->refs);
  188. out:
  189. spin_unlock(&delayed_root->lock);
  190. return node;
  191. }
  192. static struct btrfs_delayed_node *btrfs_next_delayed_node(
  193. struct btrfs_delayed_node *node)
  194. {
  195. struct btrfs_delayed_root *delayed_root;
  196. struct list_head *p;
  197. struct btrfs_delayed_node *next = NULL;
  198. delayed_root = node->root->fs_info->delayed_root;
  199. spin_lock(&delayed_root->lock);
  200. if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
  201. /* not in the list */
  202. if (list_empty(&delayed_root->node_list))
  203. goto out;
  204. p = delayed_root->node_list.next;
  205. } else if (list_is_last(&node->n_list, &delayed_root->node_list))
  206. goto out;
  207. else
  208. p = node->n_list.next;
  209. next = list_entry(p, struct btrfs_delayed_node, n_list);
  210. atomic_inc(&next->refs);
  211. out:
  212. spin_unlock(&delayed_root->lock);
  213. return next;
  214. }
  215. static void __btrfs_release_delayed_node(
  216. struct btrfs_delayed_node *delayed_node,
  217. int mod)
  218. {
  219. struct btrfs_delayed_root *delayed_root;
  220. if (!delayed_node)
  221. return;
  222. delayed_root = delayed_node->root->fs_info->delayed_root;
  223. mutex_lock(&delayed_node->mutex);
  224. if (delayed_node->count)
  225. btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
  226. else
  227. btrfs_dequeue_delayed_node(delayed_root, delayed_node);
  228. mutex_unlock(&delayed_node->mutex);
  229. if (atomic_dec_and_test(&delayed_node->refs)) {
  230. bool free = false;
  231. struct btrfs_root *root = delayed_node->root;
  232. spin_lock(&root->inode_lock);
  233. if (atomic_read(&delayed_node->refs) == 0) {
  234. radix_tree_delete(&root->delayed_nodes_tree,
  235. delayed_node->inode_id);
  236. free = true;
  237. }
  238. spin_unlock(&root->inode_lock);
  239. if (free)
  240. kmem_cache_free(delayed_node_cache, delayed_node);
  241. }
  242. }
  243. static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
  244. {
  245. __btrfs_release_delayed_node(node, 0);
  246. }
  247. static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
  248. struct btrfs_delayed_root *delayed_root)
  249. {
  250. struct list_head *p;
  251. struct btrfs_delayed_node *node = NULL;
  252. spin_lock(&delayed_root->lock);
  253. if (list_empty(&delayed_root->prepare_list))
  254. goto out;
  255. p = delayed_root->prepare_list.next;
  256. list_del_init(p);
  257. node = list_entry(p, struct btrfs_delayed_node, p_list);
  258. atomic_inc(&node->refs);
  259. out:
  260. spin_unlock(&delayed_root->lock);
  261. return node;
  262. }
  263. static inline void btrfs_release_prepared_delayed_node(
  264. struct btrfs_delayed_node *node)
  265. {
  266. __btrfs_release_delayed_node(node, 1);
  267. }
  268. static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
  269. {
  270. struct btrfs_delayed_item *item;
  271. item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
  272. if (item) {
  273. item->data_len = data_len;
  274. item->ins_or_del = 0;
  275. item->bytes_reserved = 0;
  276. item->delayed_node = NULL;
  277. atomic_set(&item->refs, 1);
  278. }
  279. return item;
  280. }
  281. /*
  282. * __btrfs_lookup_delayed_item - look up the delayed item by key
  283. * @delayed_node: pointer to the delayed node
  284. * @key: the key to look up
  285. * @prev: used to store the prev item if the right item isn't found
  286. * @next: used to store the next item if the right item isn't found
  287. *
  288. * Note: if we don't find the right item, we will return the prev item and
  289. * the next item.
  290. */
  291. static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
  292. struct rb_root *root,
  293. struct btrfs_key *key,
  294. struct btrfs_delayed_item **prev,
  295. struct btrfs_delayed_item **next)
  296. {
  297. struct rb_node *node, *prev_node = NULL;
  298. struct btrfs_delayed_item *delayed_item = NULL;
  299. int ret = 0;
  300. node = root->rb_node;
  301. while (node) {
  302. delayed_item = rb_entry(node, struct btrfs_delayed_item,
  303. rb_node);
  304. prev_node = node;
  305. ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
  306. if (ret < 0)
  307. node = node->rb_right;
  308. else if (ret > 0)
  309. node = node->rb_left;
  310. else
  311. return delayed_item;
  312. }
  313. if (prev) {
  314. if (!prev_node)
  315. *prev = NULL;
  316. else if (ret < 0)
  317. *prev = delayed_item;
  318. else if ((node = rb_prev(prev_node)) != NULL) {
  319. *prev = rb_entry(node, struct btrfs_delayed_item,
  320. rb_node);
  321. } else
  322. *prev = NULL;
  323. }
  324. if (next) {
  325. if (!prev_node)
  326. *next = NULL;
  327. else if (ret > 0)
  328. *next = delayed_item;
  329. else if ((node = rb_next(prev_node)) != NULL) {
  330. *next = rb_entry(node, struct btrfs_delayed_item,
  331. rb_node);
  332. } else
  333. *next = NULL;
  334. }
  335. return NULL;
  336. }
  337. static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
  338. struct btrfs_delayed_node *delayed_node,
  339. struct btrfs_key *key)
  340. {
  341. return __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
  342. NULL, NULL);
  343. }
  344. static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
  345. struct btrfs_delayed_item *ins,
  346. int action)
  347. {
  348. struct rb_node **p, *node;
  349. struct rb_node *parent_node = NULL;
  350. struct rb_root *root;
  351. struct btrfs_delayed_item *item;
  352. int cmp;
  353. if (action == BTRFS_DELAYED_INSERTION_ITEM)
  354. root = &delayed_node->ins_root;
  355. else if (action == BTRFS_DELAYED_DELETION_ITEM)
  356. root = &delayed_node->del_root;
  357. else
  358. BUG();
  359. p = &root->rb_node;
  360. node = &ins->rb_node;
  361. while (*p) {
  362. parent_node = *p;
  363. item = rb_entry(parent_node, struct btrfs_delayed_item,
  364. rb_node);
  365. cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
  366. if (cmp < 0)
  367. p = &(*p)->rb_right;
  368. else if (cmp > 0)
  369. p = &(*p)->rb_left;
  370. else
  371. return -EEXIST;
  372. }
  373. rb_link_node(node, parent_node, p);
  374. rb_insert_color(node, root);
  375. ins->delayed_node = delayed_node;
  376. ins->ins_or_del = action;
  377. if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
  378. action == BTRFS_DELAYED_INSERTION_ITEM &&
  379. ins->key.offset >= delayed_node->index_cnt)
  380. delayed_node->index_cnt = ins->key.offset + 1;
  381. delayed_node->count++;
  382. atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
  383. return 0;
  384. }
  385. static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
  386. struct btrfs_delayed_item *item)
  387. {
  388. return __btrfs_add_delayed_item(node, item,
  389. BTRFS_DELAYED_INSERTION_ITEM);
  390. }
  391. static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
  392. struct btrfs_delayed_item *item)
  393. {
  394. return __btrfs_add_delayed_item(node, item,
  395. BTRFS_DELAYED_DELETION_ITEM);
  396. }
  397. static void finish_one_item(struct btrfs_delayed_root *delayed_root)
  398. {
  399. int seq = atomic_inc_return(&delayed_root->items_seq);
  400. /*
  401. * atomic_dec_return implies a barrier for waitqueue_active
  402. */
  403. if ((atomic_dec_return(&delayed_root->items) <
  404. BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
  405. waitqueue_active(&delayed_root->wait))
  406. wake_up(&delayed_root->wait);
  407. }
  408. static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
  409. {
  410. struct rb_root *root;
  411. struct btrfs_delayed_root *delayed_root;
  412. delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
  413. BUG_ON(!delayed_root);
  414. BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
  415. delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
  416. if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
  417. root = &delayed_item->delayed_node->ins_root;
  418. else
  419. root = &delayed_item->delayed_node->del_root;
  420. rb_erase(&delayed_item->rb_node, root);
  421. delayed_item->delayed_node->count--;
  422. finish_one_item(delayed_root);
  423. }
  424. static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
  425. {
  426. if (item) {
  427. __btrfs_remove_delayed_item(item);
  428. if (atomic_dec_and_test(&item->refs))
  429. kfree(item);
  430. }
  431. }
  432. static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
  433. struct btrfs_delayed_node *delayed_node)
  434. {
  435. struct rb_node *p;
  436. struct btrfs_delayed_item *item = NULL;
  437. p = rb_first(&delayed_node->ins_root);
  438. if (p)
  439. item = rb_entry(p, struct btrfs_delayed_item, rb_node);
  440. return item;
  441. }
  442. static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
  443. struct btrfs_delayed_node *delayed_node)
  444. {
  445. struct rb_node *p;
  446. struct btrfs_delayed_item *item = NULL;
  447. p = rb_first(&delayed_node->del_root);
  448. if (p)
  449. item = rb_entry(p, struct btrfs_delayed_item, rb_node);
  450. return item;
  451. }
  452. static struct btrfs_delayed_item *__btrfs_next_delayed_item(
  453. struct btrfs_delayed_item *item)
  454. {
  455. struct rb_node *p;
  456. struct btrfs_delayed_item *next = NULL;
  457. p = rb_next(&item->rb_node);
  458. if (p)
  459. next = rb_entry(p, struct btrfs_delayed_item, rb_node);
  460. return next;
  461. }
  462. static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
  463. struct btrfs_root *root,
  464. struct btrfs_delayed_item *item)
  465. {
  466. struct btrfs_block_rsv *src_rsv;
  467. struct btrfs_block_rsv *dst_rsv;
  468. u64 num_bytes;
  469. int ret;
  470. if (!trans->bytes_reserved)
  471. return 0;
  472. src_rsv = trans->block_rsv;
  473. dst_rsv = &root->fs_info->delayed_block_rsv;
  474. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  475. ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
  476. if (!ret) {
  477. trace_btrfs_space_reservation(root->fs_info, "delayed_item",
  478. item->key.objectid,
  479. num_bytes, 1);
  480. item->bytes_reserved = num_bytes;
  481. }
  482. return ret;
  483. }
  484. static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
  485. struct btrfs_delayed_item *item)
  486. {
  487. struct btrfs_block_rsv *rsv;
  488. if (!item->bytes_reserved)
  489. return;
  490. rsv = &root->fs_info->delayed_block_rsv;
  491. trace_btrfs_space_reservation(root->fs_info, "delayed_item",
  492. item->key.objectid, item->bytes_reserved,
  493. 0);
  494. btrfs_block_rsv_release(root, rsv,
  495. item->bytes_reserved);
  496. }
  497. static int btrfs_delayed_inode_reserve_metadata(
  498. struct btrfs_trans_handle *trans,
  499. struct btrfs_root *root,
  500. struct inode *inode,
  501. struct btrfs_delayed_node *node)
  502. {
  503. struct btrfs_block_rsv *src_rsv;
  504. struct btrfs_block_rsv *dst_rsv;
  505. u64 num_bytes;
  506. int ret;
  507. bool release = false;
  508. src_rsv = trans->block_rsv;
  509. dst_rsv = &root->fs_info->delayed_block_rsv;
  510. num_bytes = btrfs_calc_trans_metadata_size(root, 1);
  511. /*
  512. * If our block_rsv is the delalloc block reserve then check and see if
  513. * we have our extra reservation for updating the inode. If not fall
  514. * through and try to reserve space quickly.
  515. *
  516. * We used to try and steal from the delalloc block rsv or the global
  517. * reserve, but we'd steal a full reservation, which isn't kind. We are
  518. * here through delalloc which means we've likely just cowed down close
  519. * to the leaf that contains the inode, so we would steal less just
  520. * doing the fallback inode update, so if we do end up having to steal
  521. * from the global block rsv we hopefully only steal one or two blocks
  522. * worth which is less likely to hurt us.
  523. */
  524. if (src_rsv && src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) {
  525. spin_lock(&BTRFS_I(inode)->lock);
  526. if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
  527. &BTRFS_I(inode)->runtime_flags))
  528. release = true;
  529. else
  530. src_rsv = NULL;
  531. spin_unlock(&BTRFS_I(inode)->lock);
  532. }
  533. /*
  534. * btrfs_dirty_inode will update the inode under btrfs_join_transaction
  535. * which doesn't reserve space for speed. This is a problem since we
  536. * still need to reserve space for this update, so try to reserve the
  537. * space.
  538. *
  539. * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
  540. * we're accounted for.
  541. */
  542. if (!src_rsv || (!trans->bytes_reserved &&
  543. src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
  544. ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
  545. BTRFS_RESERVE_NO_FLUSH);
  546. /*
  547. * Since we're under a transaction reserve_metadata_bytes could
  548. * try to commit the transaction which will make it return
  549. * EAGAIN to make us stop the transaction we have, so return
  550. * ENOSPC instead so that btrfs_dirty_inode knows what to do.
  551. */
  552. if (ret == -EAGAIN)
  553. ret = -ENOSPC;
  554. if (!ret) {
  555. node->bytes_reserved = num_bytes;
  556. trace_btrfs_space_reservation(root->fs_info,
  557. "delayed_inode",
  558. btrfs_ino(inode),
  559. num_bytes, 1);
  560. }
  561. return ret;
  562. }
  563. ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
  564. /*
  565. * Migrate only takes a reservation, it doesn't touch the size of the
  566. * block_rsv. This is to simplify people who don't normally have things
  567. * migrated from their block rsv. If they go to release their
  568. * reservation, that will decrease the size as well, so if migrate
  569. * reduced size we'd end up with a negative size. But for the
  570. * delalloc_meta_reserved stuff we will only know to drop 1 reservation,
  571. * but we could in fact do this reserve/migrate dance several times
  572. * between the time we did the original reservation and we'd clean it
  573. * up. So to take care of this, release the space for the meta
  574. * reservation here. I think it may be time for a documentation page on
  575. * how block rsvs. work.
  576. */
  577. if (!ret) {
  578. trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
  579. btrfs_ino(inode), num_bytes, 1);
  580. node->bytes_reserved = num_bytes;
  581. }
  582. if (release) {
  583. trace_btrfs_space_reservation(root->fs_info, "delalloc",
  584. btrfs_ino(inode), num_bytes, 0);
  585. btrfs_block_rsv_release(root, src_rsv, num_bytes);
  586. }
  587. return ret;
  588. }
  589. static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
  590. struct btrfs_delayed_node *node)
  591. {
  592. struct btrfs_block_rsv *rsv;
  593. if (!node->bytes_reserved)
  594. return;
  595. rsv = &root->fs_info->delayed_block_rsv;
  596. trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
  597. node->inode_id, node->bytes_reserved, 0);
  598. btrfs_block_rsv_release(root, rsv,
  599. node->bytes_reserved);
  600. node->bytes_reserved = 0;
  601. }
  602. /*
  603. * This helper will insert some continuous items into the same leaf according
  604. * to the free space of the leaf.
  605. */
  606. static int btrfs_batch_insert_items(struct btrfs_root *root,
  607. struct btrfs_path *path,
  608. struct btrfs_delayed_item *item)
  609. {
  610. struct btrfs_delayed_item *curr, *next;
  611. int free_space;
  612. int total_data_size = 0, total_size = 0;
  613. struct extent_buffer *leaf;
  614. char *data_ptr;
  615. struct btrfs_key *keys;
  616. u32 *data_size;
  617. struct list_head head;
  618. int slot;
  619. int nitems;
  620. int i;
  621. int ret = 0;
  622. BUG_ON(!path->nodes[0]);
  623. leaf = path->nodes[0];
  624. free_space = btrfs_leaf_free_space(root, leaf);
  625. INIT_LIST_HEAD(&head);
  626. next = item;
  627. nitems = 0;
  628. /*
  629. * count the number of the continuous items that we can insert in batch
  630. */
  631. while (total_size + next->data_len + sizeof(struct btrfs_item) <=
  632. free_space) {
  633. total_data_size += next->data_len;
  634. total_size += next->data_len + sizeof(struct btrfs_item);
  635. list_add_tail(&next->tree_list, &head);
  636. nitems++;
  637. curr = next;
  638. next = __btrfs_next_delayed_item(curr);
  639. if (!next)
  640. break;
  641. if (!btrfs_is_continuous_delayed_item(curr, next))
  642. break;
  643. }
  644. if (!nitems) {
  645. ret = 0;
  646. goto out;
  647. }
  648. /*
  649. * we need allocate some memory space, but it might cause the task
  650. * to sleep, so we set all locked nodes in the path to blocking locks
  651. * first.
  652. */
  653. btrfs_set_path_blocking(path);
  654. keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
  655. if (!keys) {
  656. ret = -ENOMEM;
  657. goto out;
  658. }
  659. data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
  660. if (!data_size) {
  661. ret = -ENOMEM;
  662. goto error;
  663. }
  664. /* get keys of all the delayed items */
  665. i = 0;
  666. list_for_each_entry(next, &head, tree_list) {
  667. keys[i] = next->key;
  668. data_size[i] = next->data_len;
  669. i++;
  670. }
  671. /* reset all the locked nodes in the patch to spinning locks. */
  672. btrfs_clear_path_blocking(path, NULL, 0);
  673. /* insert the keys of the items */
  674. setup_items_for_insert(root, path, keys, data_size,
  675. total_data_size, total_size, nitems);
  676. /* insert the dir index items */
  677. slot = path->slots[0];
  678. list_for_each_entry_safe(curr, next, &head, tree_list) {
  679. data_ptr = btrfs_item_ptr(leaf, slot, char);
  680. write_extent_buffer(leaf, &curr->data,
  681. (unsigned long)data_ptr,
  682. curr->data_len);
  683. slot++;
  684. btrfs_delayed_item_release_metadata(root, curr);
  685. list_del(&curr->tree_list);
  686. btrfs_release_delayed_item(curr);
  687. }
  688. error:
  689. kfree(data_size);
  690. kfree(keys);
  691. out:
  692. return ret;
  693. }
  694. /*
  695. * This helper can just do simple insertion that needn't extend item for new
  696. * data, such as directory name index insertion, inode insertion.
  697. */
  698. static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
  699. struct btrfs_root *root,
  700. struct btrfs_path *path,
  701. struct btrfs_delayed_item *delayed_item)
  702. {
  703. struct extent_buffer *leaf;
  704. char *ptr;
  705. int ret;
  706. ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
  707. delayed_item->data_len);
  708. if (ret < 0 && ret != -EEXIST)
  709. return ret;
  710. leaf = path->nodes[0];
  711. ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  712. write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
  713. delayed_item->data_len);
  714. btrfs_mark_buffer_dirty(leaf);
  715. btrfs_delayed_item_release_metadata(root, delayed_item);
  716. return 0;
  717. }
  718. /*
  719. * we insert an item first, then if there are some continuous items, we try
  720. * to insert those items into the same leaf.
  721. */
  722. static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
  723. struct btrfs_path *path,
  724. struct btrfs_root *root,
  725. struct btrfs_delayed_node *node)
  726. {
  727. struct btrfs_delayed_item *curr, *prev;
  728. int ret = 0;
  729. do_again:
  730. mutex_lock(&node->mutex);
  731. curr = __btrfs_first_delayed_insertion_item(node);
  732. if (!curr)
  733. goto insert_end;
  734. ret = btrfs_insert_delayed_item(trans, root, path, curr);
  735. if (ret < 0) {
  736. btrfs_release_path(path);
  737. goto insert_end;
  738. }
  739. prev = curr;
  740. curr = __btrfs_next_delayed_item(prev);
  741. if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
  742. /* insert the continuous items into the same leaf */
  743. path->slots[0]++;
  744. btrfs_batch_insert_items(root, path, curr);
  745. }
  746. btrfs_release_delayed_item(prev);
  747. btrfs_mark_buffer_dirty(path->nodes[0]);
  748. btrfs_release_path(path);
  749. mutex_unlock(&node->mutex);
  750. goto do_again;
  751. insert_end:
  752. mutex_unlock(&node->mutex);
  753. return ret;
  754. }
  755. static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
  756. struct btrfs_root *root,
  757. struct btrfs_path *path,
  758. struct btrfs_delayed_item *item)
  759. {
  760. struct btrfs_delayed_item *curr, *next;
  761. struct extent_buffer *leaf;
  762. struct btrfs_key key;
  763. struct list_head head;
  764. int nitems, i, last_item;
  765. int ret = 0;
  766. BUG_ON(!path->nodes[0]);
  767. leaf = path->nodes[0];
  768. i = path->slots[0];
  769. last_item = btrfs_header_nritems(leaf) - 1;
  770. if (i > last_item)
  771. return -ENOENT; /* FIXME: Is errno suitable? */
  772. next = item;
  773. INIT_LIST_HEAD(&head);
  774. btrfs_item_key_to_cpu(leaf, &key, i);
  775. nitems = 0;
  776. /*
  777. * count the number of the dir index items that we can delete in batch
  778. */
  779. while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
  780. list_add_tail(&next->tree_list, &head);
  781. nitems++;
  782. curr = next;
  783. next = __btrfs_next_delayed_item(curr);
  784. if (!next)
  785. break;
  786. if (!btrfs_is_continuous_delayed_item(curr, next))
  787. break;
  788. i++;
  789. if (i > last_item)
  790. break;
  791. btrfs_item_key_to_cpu(leaf, &key, i);
  792. }
  793. if (!nitems)
  794. return 0;
  795. ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
  796. if (ret)
  797. goto out;
  798. list_for_each_entry_safe(curr, next, &head, tree_list) {
  799. btrfs_delayed_item_release_metadata(root, curr);
  800. list_del(&curr->tree_list);
  801. btrfs_release_delayed_item(curr);
  802. }
  803. out:
  804. return ret;
  805. }
  806. static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
  807. struct btrfs_path *path,
  808. struct btrfs_root *root,
  809. struct btrfs_delayed_node *node)
  810. {
  811. struct btrfs_delayed_item *curr, *prev;
  812. int ret = 0;
  813. do_again:
  814. mutex_lock(&node->mutex);
  815. curr = __btrfs_first_delayed_deletion_item(node);
  816. if (!curr)
  817. goto delete_fail;
  818. ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
  819. if (ret < 0)
  820. goto delete_fail;
  821. else if (ret > 0) {
  822. /*
  823. * can't find the item which the node points to, so this node
  824. * is invalid, just drop it.
  825. */
  826. prev = curr;
  827. curr = __btrfs_next_delayed_item(prev);
  828. btrfs_release_delayed_item(prev);
  829. ret = 0;
  830. btrfs_release_path(path);
  831. if (curr) {
  832. mutex_unlock(&node->mutex);
  833. goto do_again;
  834. } else
  835. goto delete_fail;
  836. }
  837. btrfs_batch_delete_items(trans, root, path, curr);
  838. btrfs_release_path(path);
  839. mutex_unlock(&node->mutex);
  840. goto do_again;
  841. delete_fail:
  842. btrfs_release_path(path);
  843. mutex_unlock(&node->mutex);
  844. return ret;
  845. }
  846. static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
  847. {
  848. struct btrfs_delayed_root *delayed_root;
  849. if (delayed_node &&
  850. test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
  851. BUG_ON(!delayed_node->root);
  852. clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
  853. delayed_node->count--;
  854. delayed_root = delayed_node->root->fs_info->delayed_root;
  855. finish_one_item(delayed_root);
  856. }
  857. }
  858. static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
  859. {
  860. struct btrfs_delayed_root *delayed_root;
  861. ASSERT(delayed_node->root);
  862. clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
  863. delayed_node->count--;
  864. delayed_root = delayed_node->root->fs_info->delayed_root;
  865. finish_one_item(delayed_root);
  866. }
  867. static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
  868. struct btrfs_root *root,
  869. struct btrfs_path *path,
  870. struct btrfs_delayed_node *node)
  871. {
  872. struct btrfs_key key;
  873. struct btrfs_inode_item *inode_item;
  874. struct extent_buffer *leaf;
  875. int mod;
  876. int ret;
  877. key.objectid = node->inode_id;
  878. key.type = BTRFS_INODE_ITEM_KEY;
  879. key.offset = 0;
  880. if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
  881. mod = -1;
  882. else
  883. mod = 1;
  884. ret = btrfs_lookup_inode(trans, root, path, &key, mod);
  885. if (ret > 0) {
  886. btrfs_release_path(path);
  887. return -ENOENT;
  888. } else if (ret < 0) {
  889. return ret;
  890. }
  891. leaf = path->nodes[0];
  892. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  893. struct btrfs_inode_item);
  894. write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
  895. sizeof(struct btrfs_inode_item));
  896. btrfs_mark_buffer_dirty(leaf);
  897. if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
  898. goto no_iref;
  899. path->slots[0]++;
  900. if (path->slots[0] >= btrfs_header_nritems(leaf))
  901. goto search;
  902. again:
  903. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  904. if (key.objectid != node->inode_id)
  905. goto out;
  906. if (key.type != BTRFS_INODE_REF_KEY &&
  907. key.type != BTRFS_INODE_EXTREF_KEY)
  908. goto out;
  909. /*
  910. * Delayed iref deletion is for the inode who has only one link,
  911. * so there is only one iref. The case that several irefs are
  912. * in the same item doesn't exist.
  913. */
  914. btrfs_del_item(trans, root, path);
  915. out:
  916. btrfs_release_delayed_iref(node);
  917. no_iref:
  918. btrfs_release_path(path);
  919. err_out:
  920. btrfs_delayed_inode_release_metadata(root, node);
  921. btrfs_release_delayed_inode(node);
  922. return ret;
  923. search:
  924. btrfs_release_path(path);
  925. key.type = BTRFS_INODE_EXTREF_KEY;
  926. key.offset = -1;
  927. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  928. if (ret < 0)
  929. goto err_out;
  930. ASSERT(ret);
  931. ret = 0;
  932. leaf = path->nodes[0];
  933. path->slots[0]--;
  934. goto again;
  935. }
  936. static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
  937. struct btrfs_root *root,
  938. struct btrfs_path *path,
  939. struct btrfs_delayed_node *node)
  940. {
  941. int ret;
  942. mutex_lock(&node->mutex);
  943. if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
  944. mutex_unlock(&node->mutex);
  945. return 0;
  946. }
  947. ret = __btrfs_update_delayed_inode(trans, root, path, node);
  948. mutex_unlock(&node->mutex);
  949. return ret;
  950. }
  951. static inline int
  952. __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
  953. struct btrfs_path *path,
  954. struct btrfs_delayed_node *node)
  955. {
  956. int ret;
  957. ret = btrfs_insert_delayed_items(trans, path, node->root, node);
  958. if (ret)
  959. return ret;
  960. ret = btrfs_delete_delayed_items(trans, path, node->root, node);
  961. if (ret)
  962. return ret;
  963. ret = btrfs_update_delayed_inode(trans, node->root, path, node);
  964. return ret;
  965. }
  966. /*
  967. * Called when committing the transaction.
  968. * Returns 0 on success.
  969. * Returns < 0 on error and returns with an aborted transaction with any
  970. * outstanding delayed items cleaned up.
  971. */
  972. static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
  973. struct btrfs_root *root, int nr)
  974. {
  975. struct btrfs_delayed_root *delayed_root;
  976. struct btrfs_delayed_node *curr_node, *prev_node;
  977. struct btrfs_path *path;
  978. struct btrfs_block_rsv *block_rsv;
  979. int ret = 0;
  980. bool count = (nr > 0);
  981. if (trans->aborted)
  982. return -EIO;
  983. path = btrfs_alloc_path();
  984. if (!path)
  985. return -ENOMEM;
  986. path->leave_spinning = 1;
  987. block_rsv = trans->block_rsv;
  988. trans->block_rsv = &root->fs_info->delayed_block_rsv;
  989. delayed_root = btrfs_get_delayed_root(root);
  990. curr_node = btrfs_first_delayed_node(delayed_root);
  991. while (curr_node && (!count || (count && nr--))) {
  992. ret = __btrfs_commit_inode_delayed_items(trans, path,
  993. curr_node);
  994. if (ret) {
  995. btrfs_release_delayed_node(curr_node);
  996. curr_node = NULL;
  997. btrfs_abort_transaction(trans, ret);
  998. break;
  999. }
  1000. prev_node = curr_node;
  1001. curr_node = btrfs_next_delayed_node(curr_node);
  1002. btrfs_release_delayed_node(prev_node);
  1003. }
  1004. if (curr_node)
  1005. btrfs_release_delayed_node(curr_node);
  1006. btrfs_free_path(path);
  1007. trans->block_rsv = block_rsv;
  1008. return ret;
  1009. }
  1010. int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
  1011. struct btrfs_root *root)
  1012. {
  1013. return __btrfs_run_delayed_items(trans, root, -1);
  1014. }
  1015. int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
  1016. struct btrfs_root *root, int nr)
  1017. {
  1018. return __btrfs_run_delayed_items(trans, root, nr);
  1019. }
  1020. int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
  1021. struct inode *inode)
  1022. {
  1023. struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
  1024. struct btrfs_path *path;
  1025. struct btrfs_block_rsv *block_rsv;
  1026. int ret;
  1027. if (!delayed_node)
  1028. return 0;
  1029. mutex_lock(&delayed_node->mutex);
  1030. if (!delayed_node->count) {
  1031. mutex_unlock(&delayed_node->mutex);
  1032. btrfs_release_delayed_node(delayed_node);
  1033. return 0;
  1034. }
  1035. mutex_unlock(&delayed_node->mutex);
  1036. path = btrfs_alloc_path();
  1037. if (!path) {
  1038. btrfs_release_delayed_node(delayed_node);
  1039. return -ENOMEM;
  1040. }
  1041. path->leave_spinning = 1;
  1042. block_rsv = trans->block_rsv;
  1043. trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
  1044. ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
  1045. btrfs_release_delayed_node(delayed_node);
  1046. btrfs_free_path(path);
  1047. trans->block_rsv = block_rsv;
  1048. return ret;
  1049. }
  1050. int btrfs_commit_inode_delayed_inode(struct inode *inode)
  1051. {
  1052. struct btrfs_trans_handle *trans;
  1053. struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
  1054. struct btrfs_path *path;
  1055. struct btrfs_block_rsv *block_rsv;
  1056. int ret;
  1057. if (!delayed_node)
  1058. return 0;
  1059. mutex_lock(&delayed_node->mutex);
  1060. if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
  1061. mutex_unlock(&delayed_node->mutex);
  1062. btrfs_release_delayed_node(delayed_node);
  1063. return 0;
  1064. }
  1065. mutex_unlock(&delayed_node->mutex);
  1066. trans = btrfs_join_transaction(delayed_node->root);
  1067. if (IS_ERR(trans)) {
  1068. ret = PTR_ERR(trans);
  1069. goto out;
  1070. }
  1071. path = btrfs_alloc_path();
  1072. if (!path) {
  1073. ret = -ENOMEM;
  1074. goto trans_out;
  1075. }
  1076. path->leave_spinning = 1;
  1077. block_rsv = trans->block_rsv;
  1078. trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
  1079. mutex_lock(&delayed_node->mutex);
  1080. if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
  1081. ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
  1082. path, delayed_node);
  1083. else
  1084. ret = 0;
  1085. mutex_unlock(&delayed_node->mutex);
  1086. btrfs_free_path(path);
  1087. trans->block_rsv = block_rsv;
  1088. trans_out:
  1089. btrfs_end_transaction(trans, delayed_node->root);
  1090. btrfs_btree_balance_dirty(delayed_node->root);
  1091. out:
  1092. btrfs_release_delayed_node(delayed_node);
  1093. return ret;
  1094. }
  1095. void btrfs_remove_delayed_node(struct inode *inode)
  1096. {
  1097. struct btrfs_delayed_node *delayed_node;
  1098. delayed_node = ACCESS_ONCE(BTRFS_I(inode)->delayed_node);
  1099. if (!delayed_node)
  1100. return;
  1101. BTRFS_I(inode)->delayed_node = NULL;
  1102. btrfs_release_delayed_node(delayed_node);
  1103. }
  1104. struct btrfs_async_delayed_work {
  1105. struct btrfs_delayed_root *delayed_root;
  1106. int nr;
  1107. struct btrfs_work work;
  1108. };
  1109. static void btrfs_async_run_delayed_root(struct btrfs_work *work)
  1110. {
  1111. struct btrfs_async_delayed_work *async_work;
  1112. struct btrfs_delayed_root *delayed_root;
  1113. struct btrfs_trans_handle *trans;
  1114. struct btrfs_path *path;
  1115. struct btrfs_delayed_node *delayed_node = NULL;
  1116. struct btrfs_root *root;
  1117. struct btrfs_block_rsv *block_rsv;
  1118. int total_done = 0;
  1119. async_work = container_of(work, struct btrfs_async_delayed_work, work);
  1120. delayed_root = async_work->delayed_root;
  1121. path = btrfs_alloc_path();
  1122. if (!path)
  1123. goto out;
  1124. again:
  1125. if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
  1126. goto free_path;
  1127. delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
  1128. if (!delayed_node)
  1129. goto free_path;
  1130. path->leave_spinning = 1;
  1131. root = delayed_node->root;
  1132. trans = btrfs_join_transaction(root);
  1133. if (IS_ERR(trans))
  1134. goto release_path;
  1135. block_rsv = trans->block_rsv;
  1136. trans->block_rsv = &root->fs_info->delayed_block_rsv;
  1137. __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
  1138. trans->block_rsv = block_rsv;
  1139. btrfs_end_transaction(trans, root);
  1140. btrfs_btree_balance_dirty_nodelay(root);
  1141. release_path:
  1142. btrfs_release_path(path);
  1143. total_done++;
  1144. btrfs_release_prepared_delayed_node(delayed_node);
  1145. if ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK) ||
  1146. total_done < async_work->nr)
  1147. goto again;
  1148. free_path:
  1149. btrfs_free_path(path);
  1150. out:
  1151. wake_up(&delayed_root->wait);
  1152. kfree(async_work);
  1153. }
  1154. static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
  1155. struct btrfs_fs_info *fs_info, int nr)
  1156. {
  1157. struct btrfs_async_delayed_work *async_work;
  1158. if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND ||
  1159. btrfs_workqueue_normal_congested(fs_info->delayed_workers))
  1160. return 0;
  1161. async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
  1162. if (!async_work)
  1163. return -ENOMEM;
  1164. async_work->delayed_root = delayed_root;
  1165. btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper,
  1166. btrfs_async_run_delayed_root, NULL, NULL);
  1167. async_work->nr = nr;
  1168. btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
  1169. return 0;
  1170. }
  1171. void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
  1172. {
  1173. struct btrfs_delayed_root *delayed_root;
  1174. delayed_root = btrfs_get_delayed_root(root);
  1175. WARN_ON(btrfs_first_delayed_node(delayed_root));
  1176. }
  1177. static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
  1178. {
  1179. int val = atomic_read(&delayed_root->items_seq);
  1180. if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
  1181. return 1;
  1182. if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
  1183. return 1;
  1184. return 0;
  1185. }
  1186. void btrfs_balance_delayed_items(struct btrfs_root *root)
  1187. {
  1188. struct btrfs_delayed_root *delayed_root;
  1189. struct btrfs_fs_info *fs_info = root->fs_info;
  1190. delayed_root = btrfs_get_delayed_root(root);
  1191. if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
  1192. return;
  1193. if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
  1194. int seq;
  1195. int ret;
  1196. seq = atomic_read(&delayed_root->items_seq);
  1197. ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
  1198. if (ret)
  1199. return;
  1200. wait_event_interruptible(delayed_root->wait,
  1201. could_end_wait(delayed_root, seq));
  1202. return;
  1203. }
  1204. btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
  1205. }
  1206. /* Will return 0 or -ENOMEM */
  1207. int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
  1208. struct btrfs_root *root, const char *name,
  1209. int name_len, struct inode *dir,
  1210. struct btrfs_disk_key *disk_key, u8 type,
  1211. u64 index)
  1212. {
  1213. struct btrfs_delayed_node *delayed_node;
  1214. struct btrfs_delayed_item *delayed_item;
  1215. struct btrfs_dir_item *dir_item;
  1216. int ret;
  1217. delayed_node = btrfs_get_or_create_delayed_node(dir);
  1218. if (IS_ERR(delayed_node))
  1219. return PTR_ERR(delayed_node);
  1220. delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
  1221. if (!delayed_item) {
  1222. ret = -ENOMEM;
  1223. goto release_node;
  1224. }
  1225. delayed_item->key.objectid = btrfs_ino(dir);
  1226. delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
  1227. delayed_item->key.offset = index;
  1228. dir_item = (struct btrfs_dir_item *)delayed_item->data;
  1229. dir_item->location = *disk_key;
  1230. btrfs_set_stack_dir_transid(dir_item, trans->transid);
  1231. btrfs_set_stack_dir_data_len(dir_item, 0);
  1232. btrfs_set_stack_dir_name_len(dir_item, name_len);
  1233. btrfs_set_stack_dir_type(dir_item, type);
  1234. memcpy((char *)(dir_item + 1), name, name_len);
  1235. ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
  1236. /*
  1237. * we have reserved enough space when we start a new transaction,
  1238. * so reserving metadata failure is impossible
  1239. */
  1240. BUG_ON(ret);
  1241. mutex_lock(&delayed_node->mutex);
  1242. ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
  1243. if (unlikely(ret)) {
  1244. btrfs_err(root->fs_info,
  1245. "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
  1246. name_len, name, delayed_node->root->objectid,
  1247. delayed_node->inode_id, ret);
  1248. BUG();
  1249. }
  1250. mutex_unlock(&delayed_node->mutex);
  1251. release_node:
  1252. btrfs_release_delayed_node(delayed_node);
  1253. return ret;
  1254. }
  1255. static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
  1256. struct btrfs_delayed_node *node,
  1257. struct btrfs_key *key)
  1258. {
  1259. struct btrfs_delayed_item *item;
  1260. mutex_lock(&node->mutex);
  1261. item = __btrfs_lookup_delayed_insertion_item(node, key);
  1262. if (!item) {
  1263. mutex_unlock(&node->mutex);
  1264. return 1;
  1265. }
  1266. btrfs_delayed_item_release_metadata(root, item);
  1267. btrfs_release_delayed_item(item);
  1268. mutex_unlock(&node->mutex);
  1269. return 0;
  1270. }
  1271. int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
  1272. struct btrfs_root *root, struct inode *dir,
  1273. u64 index)
  1274. {
  1275. struct btrfs_delayed_node *node;
  1276. struct btrfs_delayed_item *item;
  1277. struct btrfs_key item_key;
  1278. int ret;
  1279. node = btrfs_get_or_create_delayed_node(dir);
  1280. if (IS_ERR(node))
  1281. return PTR_ERR(node);
  1282. item_key.objectid = btrfs_ino(dir);
  1283. item_key.type = BTRFS_DIR_INDEX_KEY;
  1284. item_key.offset = index;
  1285. ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
  1286. if (!ret)
  1287. goto end;
  1288. item = btrfs_alloc_delayed_item(0);
  1289. if (!item) {
  1290. ret = -ENOMEM;
  1291. goto end;
  1292. }
  1293. item->key = item_key;
  1294. ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
  1295. /*
  1296. * we have reserved enough space when we start a new transaction,
  1297. * so reserving metadata failure is impossible.
  1298. */
  1299. BUG_ON(ret);
  1300. mutex_lock(&node->mutex);
  1301. ret = __btrfs_add_delayed_deletion_item(node, item);
  1302. if (unlikely(ret)) {
  1303. btrfs_err(root->fs_info,
  1304. "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
  1305. index, node->root->objectid, node->inode_id, ret);
  1306. BUG();
  1307. }
  1308. mutex_unlock(&node->mutex);
  1309. end:
  1310. btrfs_release_delayed_node(node);
  1311. return ret;
  1312. }
  1313. int btrfs_inode_delayed_dir_index_count(struct inode *inode)
  1314. {
  1315. struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
  1316. if (!delayed_node)
  1317. return -ENOENT;
  1318. /*
  1319. * Since we have held i_mutex of this directory, it is impossible that
  1320. * a new directory index is added into the delayed node and index_cnt
  1321. * is updated now. So we needn't lock the delayed node.
  1322. */
  1323. if (!delayed_node->index_cnt) {
  1324. btrfs_release_delayed_node(delayed_node);
  1325. return -EINVAL;
  1326. }
  1327. BTRFS_I(inode)->index_cnt = delayed_node->index_cnt;
  1328. btrfs_release_delayed_node(delayed_node);
  1329. return 0;
  1330. }
  1331. bool btrfs_readdir_get_delayed_items(struct inode *inode,
  1332. struct list_head *ins_list,
  1333. struct list_head *del_list)
  1334. {
  1335. struct btrfs_delayed_node *delayed_node;
  1336. struct btrfs_delayed_item *item;
  1337. delayed_node = btrfs_get_delayed_node(inode);
  1338. if (!delayed_node)
  1339. return false;
  1340. /*
  1341. * We can only do one readdir with delayed items at a time because of
  1342. * item->readdir_list.
  1343. */
  1344. inode_unlock_shared(inode);
  1345. inode_lock(inode);
  1346. mutex_lock(&delayed_node->mutex);
  1347. item = __btrfs_first_delayed_insertion_item(delayed_node);
  1348. while (item) {
  1349. atomic_inc(&item->refs);
  1350. list_add_tail(&item->readdir_list, ins_list);
  1351. item = __btrfs_next_delayed_item(item);
  1352. }
  1353. item = __btrfs_first_delayed_deletion_item(delayed_node);
  1354. while (item) {
  1355. atomic_inc(&item->refs);
  1356. list_add_tail(&item->readdir_list, del_list);
  1357. item = __btrfs_next_delayed_item(item);
  1358. }
  1359. mutex_unlock(&delayed_node->mutex);
  1360. /*
  1361. * This delayed node is still cached in the btrfs inode, so refs
  1362. * must be > 1 now, and we needn't check it is going to be freed
  1363. * or not.
  1364. *
  1365. * Besides that, this function is used to read dir, we do not
  1366. * insert/delete delayed items in this period. So we also needn't
  1367. * requeue or dequeue this delayed node.
  1368. */
  1369. atomic_dec(&delayed_node->refs);
  1370. return true;
  1371. }
  1372. void btrfs_readdir_put_delayed_items(struct inode *inode,
  1373. struct list_head *ins_list,
  1374. struct list_head *del_list)
  1375. {
  1376. struct btrfs_delayed_item *curr, *next;
  1377. list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
  1378. list_del(&curr->readdir_list);
  1379. if (atomic_dec_and_test(&curr->refs))
  1380. kfree(curr);
  1381. }
  1382. list_for_each_entry_safe(curr, next, del_list, readdir_list) {
  1383. list_del(&curr->readdir_list);
  1384. if (atomic_dec_and_test(&curr->refs))
  1385. kfree(curr);
  1386. }
  1387. /*
  1388. * The VFS is going to do up_read(), so we need to downgrade back to a
  1389. * read lock.
  1390. */
  1391. downgrade_write(&inode->i_rwsem);
  1392. }
  1393. int btrfs_should_delete_dir_index(struct list_head *del_list,
  1394. u64 index)
  1395. {
  1396. struct btrfs_delayed_item *curr, *next;
  1397. int ret;
  1398. if (list_empty(del_list))
  1399. return 0;
  1400. list_for_each_entry_safe(curr, next, del_list, readdir_list) {
  1401. if (curr->key.offset > index)
  1402. break;
  1403. list_del(&curr->readdir_list);
  1404. ret = (curr->key.offset == index);
  1405. if (atomic_dec_and_test(&curr->refs))
  1406. kfree(curr);
  1407. if (ret)
  1408. return 1;
  1409. else
  1410. continue;
  1411. }
  1412. return 0;
  1413. }
  1414. /*
  1415. * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
  1416. *
  1417. */
  1418. int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
  1419. struct list_head *ins_list, bool *emitted)
  1420. {
  1421. struct btrfs_dir_item *di;
  1422. struct btrfs_delayed_item *curr, *next;
  1423. struct btrfs_key location;
  1424. char *name;
  1425. int name_len;
  1426. int over = 0;
  1427. unsigned char d_type;
  1428. if (list_empty(ins_list))
  1429. return 0;
  1430. /*
  1431. * Changing the data of the delayed item is impossible. So
  1432. * we needn't lock them. And we have held i_mutex of the
  1433. * directory, nobody can delete any directory indexes now.
  1434. */
  1435. list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
  1436. list_del(&curr->readdir_list);
  1437. if (curr->key.offset < ctx->pos) {
  1438. if (atomic_dec_and_test(&curr->refs))
  1439. kfree(curr);
  1440. continue;
  1441. }
  1442. ctx->pos = curr->key.offset;
  1443. di = (struct btrfs_dir_item *)curr->data;
  1444. name = (char *)(di + 1);
  1445. name_len = btrfs_stack_dir_name_len(di);
  1446. d_type = btrfs_filetype_table[di->type];
  1447. btrfs_disk_key_to_cpu(&location, &di->location);
  1448. over = !dir_emit(ctx, name, name_len,
  1449. location.objectid, d_type);
  1450. if (atomic_dec_and_test(&curr->refs))
  1451. kfree(curr);
  1452. if (over)
  1453. return 1;
  1454. *emitted = true;
  1455. }
  1456. return 0;
  1457. }
  1458. static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
  1459. struct btrfs_inode_item *inode_item,
  1460. struct inode *inode)
  1461. {
  1462. btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
  1463. btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
  1464. btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
  1465. btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
  1466. btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
  1467. btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
  1468. btrfs_set_stack_inode_generation(inode_item,
  1469. BTRFS_I(inode)->generation);
  1470. btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
  1471. btrfs_set_stack_inode_transid(inode_item, trans->transid);
  1472. btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
  1473. btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
  1474. btrfs_set_stack_inode_block_group(inode_item, 0);
  1475. btrfs_set_stack_timespec_sec(&inode_item->atime,
  1476. inode->i_atime.tv_sec);
  1477. btrfs_set_stack_timespec_nsec(&inode_item->atime,
  1478. inode->i_atime.tv_nsec);
  1479. btrfs_set_stack_timespec_sec(&inode_item->mtime,
  1480. inode->i_mtime.tv_sec);
  1481. btrfs_set_stack_timespec_nsec(&inode_item->mtime,
  1482. inode->i_mtime.tv_nsec);
  1483. btrfs_set_stack_timespec_sec(&inode_item->ctime,
  1484. inode->i_ctime.tv_sec);
  1485. btrfs_set_stack_timespec_nsec(&inode_item->ctime,
  1486. inode->i_ctime.tv_nsec);
  1487. btrfs_set_stack_timespec_sec(&inode_item->otime,
  1488. BTRFS_I(inode)->i_otime.tv_sec);
  1489. btrfs_set_stack_timespec_nsec(&inode_item->otime,
  1490. BTRFS_I(inode)->i_otime.tv_nsec);
  1491. }
  1492. int btrfs_fill_inode(struct inode *inode, u32 *rdev)
  1493. {
  1494. struct btrfs_delayed_node *delayed_node;
  1495. struct btrfs_inode_item *inode_item;
  1496. delayed_node = btrfs_get_delayed_node(inode);
  1497. if (!delayed_node)
  1498. return -ENOENT;
  1499. mutex_lock(&delayed_node->mutex);
  1500. if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
  1501. mutex_unlock(&delayed_node->mutex);
  1502. btrfs_release_delayed_node(delayed_node);
  1503. return -ENOENT;
  1504. }
  1505. inode_item = &delayed_node->inode_item;
  1506. i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
  1507. i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
  1508. btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
  1509. inode->i_mode = btrfs_stack_inode_mode(inode_item);
  1510. set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
  1511. inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
  1512. BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
  1513. BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
  1514. inode->i_version = btrfs_stack_inode_sequence(inode_item);
  1515. inode->i_rdev = 0;
  1516. *rdev = btrfs_stack_inode_rdev(inode_item);
  1517. BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
  1518. inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
  1519. inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
  1520. inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
  1521. inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
  1522. inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
  1523. inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
  1524. BTRFS_I(inode)->i_otime.tv_sec =
  1525. btrfs_stack_timespec_sec(&inode_item->otime);
  1526. BTRFS_I(inode)->i_otime.tv_nsec =
  1527. btrfs_stack_timespec_nsec(&inode_item->otime);
  1528. inode->i_generation = BTRFS_I(inode)->generation;
  1529. BTRFS_I(inode)->index_cnt = (u64)-1;
  1530. mutex_unlock(&delayed_node->mutex);
  1531. btrfs_release_delayed_node(delayed_node);
  1532. return 0;
  1533. }
  1534. int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
  1535. struct btrfs_root *root, struct inode *inode)
  1536. {
  1537. struct btrfs_delayed_node *delayed_node;
  1538. int ret = 0;
  1539. delayed_node = btrfs_get_or_create_delayed_node(inode);
  1540. if (IS_ERR(delayed_node))
  1541. return PTR_ERR(delayed_node);
  1542. mutex_lock(&delayed_node->mutex);
  1543. if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
  1544. fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
  1545. goto release_node;
  1546. }
  1547. ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
  1548. delayed_node);
  1549. if (ret)
  1550. goto release_node;
  1551. fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
  1552. set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
  1553. delayed_node->count++;
  1554. atomic_inc(&root->fs_info->delayed_root->items);
  1555. release_node:
  1556. mutex_unlock(&delayed_node->mutex);
  1557. btrfs_release_delayed_node(delayed_node);
  1558. return ret;
  1559. }
  1560. int btrfs_delayed_delete_inode_ref(struct inode *inode)
  1561. {
  1562. struct btrfs_delayed_node *delayed_node;
  1563. /*
  1564. * we don't do delayed inode updates during log recovery because it
  1565. * leads to enospc problems. This means we also can't do
  1566. * delayed inode refs
  1567. */
  1568. if (test_bit(BTRFS_FS_LOG_RECOVERING,
  1569. &BTRFS_I(inode)->root->fs_info->flags))
  1570. return -EAGAIN;
  1571. delayed_node = btrfs_get_or_create_delayed_node(inode);
  1572. if (IS_ERR(delayed_node))
  1573. return PTR_ERR(delayed_node);
  1574. /*
  1575. * We don't reserve space for inode ref deletion is because:
  1576. * - We ONLY do async inode ref deletion for the inode who has only
  1577. * one link(i_nlink == 1), it means there is only one inode ref.
  1578. * And in most case, the inode ref and the inode item are in the
  1579. * same leaf, and we will deal with them at the same time.
  1580. * Since we are sure we will reserve the space for the inode item,
  1581. * it is unnecessary to reserve space for inode ref deletion.
  1582. * - If the inode ref and the inode item are not in the same leaf,
  1583. * We also needn't worry about enospc problem, because we reserve
  1584. * much more space for the inode update than it needs.
  1585. * - At the worst, we can steal some space from the global reservation.
  1586. * It is very rare.
  1587. */
  1588. mutex_lock(&delayed_node->mutex);
  1589. if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
  1590. goto release_node;
  1591. set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
  1592. delayed_node->count++;
  1593. atomic_inc(&BTRFS_I(inode)->root->fs_info->delayed_root->items);
  1594. release_node:
  1595. mutex_unlock(&delayed_node->mutex);
  1596. btrfs_release_delayed_node(delayed_node);
  1597. return 0;
  1598. }
  1599. static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
  1600. {
  1601. struct btrfs_root *root = delayed_node->root;
  1602. struct btrfs_delayed_item *curr_item, *prev_item;
  1603. mutex_lock(&delayed_node->mutex);
  1604. curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
  1605. while (curr_item) {
  1606. btrfs_delayed_item_release_metadata(root, curr_item);
  1607. prev_item = curr_item;
  1608. curr_item = __btrfs_next_delayed_item(prev_item);
  1609. btrfs_release_delayed_item(prev_item);
  1610. }
  1611. curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
  1612. while (curr_item) {
  1613. btrfs_delayed_item_release_metadata(root, curr_item);
  1614. prev_item = curr_item;
  1615. curr_item = __btrfs_next_delayed_item(prev_item);
  1616. btrfs_release_delayed_item(prev_item);
  1617. }
  1618. if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
  1619. btrfs_release_delayed_iref(delayed_node);
  1620. if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
  1621. btrfs_delayed_inode_release_metadata(root, delayed_node);
  1622. btrfs_release_delayed_inode(delayed_node);
  1623. }
  1624. mutex_unlock(&delayed_node->mutex);
  1625. }
  1626. void btrfs_kill_delayed_inode_items(struct inode *inode)
  1627. {
  1628. struct btrfs_delayed_node *delayed_node;
  1629. delayed_node = btrfs_get_delayed_node(inode);
  1630. if (!delayed_node)
  1631. return;
  1632. __btrfs_kill_delayed_node(delayed_node);
  1633. btrfs_release_delayed_node(delayed_node);
  1634. }
  1635. void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
  1636. {
  1637. u64 inode_id = 0;
  1638. struct btrfs_delayed_node *delayed_nodes[8];
  1639. int i, n;
  1640. while (1) {
  1641. spin_lock(&root->inode_lock);
  1642. n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
  1643. (void **)delayed_nodes, inode_id,
  1644. ARRAY_SIZE(delayed_nodes));
  1645. if (!n) {
  1646. spin_unlock(&root->inode_lock);
  1647. break;
  1648. }
  1649. inode_id = delayed_nodes[n - 1]->inode_id + 1;
  1650. for (i = 0; i < n; i++)
  1651. atomic_inc(&delayed_nodes[i]->refs);
  1652. spin_unlock(&root->inode_lock);
  1653. for (i = 0; i < n; i++) {
  1654. __btrfs_kill_delayed_node(delayed_nodes[i]);
  1655. btrfs_release_delayed_node(delayed_nodes[i]);
  1656. }
  1657. }
  1658. }
  1659. void btrfs_destroy_delayed_inodes(struct btrfs_root *root)
  1660. {
  1661. struct btrfs_delayed_root *delayed_root;
  1662. struct btrfs_delayed_node *curr_node, *prev_node;
  1663. delayed_root = btrfs_get_delayed_root(root);
  1664. curr_node = btrfs_first_delayed_node(delayed_root);
  1665. while (curr_node) {
  1666. __btrfs_kill_delayed_node(curr_node);
  1667. prev_node = curr_node;
  1668. curr_node = btrfs_next_delayed_node(curr_node);
  1669. btrfs_release_delayed_node(prev_node);
  1670. }
  1671. }