stree.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. /*
  5. * Written by Anatoly P. Pinchuk pap@namesys.botik.ru
  6. * Programm System Institute
  7. * Pereslavl-Zalessky Russia
  8. */
  9. /*
  10. * This file contains functions dealing with S+tree
  11. *
  12. * B_IS_IN_TREE
  13. * copy_item_head
  14. * comp_short_keys
  15. * comp_keys
  16. * comp_short_le_keys
  17. * le_key2cpu_key
  18. * comp_le_keys
  19. * bin_search
  20. * get_lkey
  21. * get_rkey
  22. * key_in_buffer
  23. * decrement_bcount
  24. * decrement_counters_in_path
  25. * reiserfs_check_path
  26. * pathrelse_and_restore
  27. * pathrelse
  28. * search_by_key_reada
  29. * search_by_key
  30. * search_for_position_by_key
  31. * comp_items
  32. * prepare_for_direct_item
  33. * prepare_for_direntry_item
  34. * prepare_for_delete_or_cut
  35. * calc_deleted_bytes_number
  36. * init_tb_struct
  37. * padd_item
  38. * reiserfs_delete_item
  39. * reiserfs_delete_solid_item
  40. * reiserfs_delete_object
  41. * maybe_indirect_to_direct
  42. * indirect_to_direct_roll_back
  43. * reiserfs_cut_from_item
  44. * truncate_directory
  45. * reiserfs_do_truncate
  46. * reiserfs_paste_into_item
  47. * reiserfs_insert_item
  48. */
  49. #include <linux/time.h>
  50. #include <linux/string.h>
  51. #include <linux/pagemap.h>
  52. #include <linux/reiserfs_fs.h>
  53. #include <linux/smp_lock.h>
  54. #include <linux/buffer_head.h>
  55. #include <linux/quotaops.h>
  56. /* Does the buffer contain a disk block which is in the tree. */
  57. inline int B_IS_IN_TREE(const struct buffer_head *p_s_bh)
  58. {
  59. RFALSE(B_LEVEL(p_s_bh) > MAX_HEIGHT,
  60. "PAP-1010: block (%b) has too big level (%z)", p_s_bh, p_s_bh);
  61. return (B_LEVEL(p_s_bh) != FREE_LEVEL);
  62. }
  63. //
  64. // to gets item head in le form
  65. //
  66. inline void copy_item_head(struct item_head *p_v_to,
  67. const struct item_head *p_v_from)
  68. {
  69. memcpy(p_v_to, p_v_from, IH_SIZE);
  70. }
  71. /* k1 is pointer to on-disk structure which is stored in little-endian
  72. form. k2 is pointer to cpu variable. For key of items of the same
  73. object this returns 0.
  74. Returns: -1 if key1 < key2
  75. 0 if key1 == key2
  76. 1 if key1 > key2 */
  77. inline int comp_short_keys(const struct reiserfs_key *le_key,
  78. const struct cpu_key *cpu_key)
  79. {
  80. __u32 n;
  81. n = le32_to_cpu(le_key->k_dir_id);
  82. if (n < cpu_key->on_disk_key.k_dir_id)
  83. return -1;
  84. if (n > cpu_key->on_disk_key.k_dir_id)
  85. return 1;
  86. n = le32_to_cpu(le_key->k_objectid);
  87. if (n < cpu_key->on_disk_key.k_objectid)
  88. return -1;
  89. if (n > cpu_key->on_disk_key.k_objectid)
  90. return 1;
  91. return 0;
  92. }
  93. /* k1 is pointer to on-disk structure which is stored in little-endian
  94. form. k2 is pointer to cpu variable.
  95. Compare keys using all 4 key fields.
  96. Returns: -1 if key1 < key2 0
  97. if key1 = key2 1 if key1 > key2 */
  98. static inline int comp_keys(const struct reiserfs_key *le_key,
  99. const struct cpu_key *cpu_key)
  100. {
  101. int retval;
  102. retval = comp_short_keys(le_key, cpu_key);
  103. if (retval)
  104. return retval;
  105. if (le_key_k_offset(le_key_version(le_key), le_key) <
  106. cpu_key_k_offset(cpu_key))
  107. return -1;
  108. if (le_key_k_offset(le_key_version(le_key), le_key) >
  109. cpu_key_k_offset(cpu_key))
  110. return 1;
  111. if (cpu_key->key_length == 3)
  112. return 0;
  113. /* this part is needed only when tail conversion is in progress */
  114. if (le_key_k_type(le_key_version(le_key), le_key) <
  115. cpu_key_k_type(cpu_key))
  116. return -1;
  117. if (le_key_k_type(le_key_version(le_key), le_key) >
  118. cpu_key_k_type(cpu_key))
  119. return 1;
  120. return 0;
  121. }
  122. inline int comp_short_le_keys(const struct reiserfs_key *key1,
  123. const struct reiserfs_key *key2)
  124. {
  125. __u32 *p_s_1_u32, *p_s_2_u32;
  126. int n_key_length = REISERFS_SHORT_KEY_LEN;
  127. p_s_1_u32 = (__u32 *) key1;
  128. p_s_2_u32 = (__u32 *) key2;
  129. for (; n_key_length--; ++p_s_1_u32, ++p_s_2_u32) {
  130. if (le32_to_cpu(*p_s_1_u32) < le32_to_cpu(*p_s_2_u32))
  131. return -1;
  132. if (le32_to_cpu(*p_s_1_u32) > le32_to_cpu(*p_s_2_u32))
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
  138. {
  139. int version;
  140. to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
  141. to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
  142. // find out version of the key
  143. version = le_key_version(from);
  144. to->version = version;
  145. to->on_disk_key.k_offset = le_key_k_offset(version, from);
  146. to->on_disk_key.k_type = le_key_k_type(version, from);
  147. }
  148. // this does not say which one is bigger, it only returns 1 if keys
  149. // are not equal, 0 otherwise
  150. inline int comp_le_keys(const struct reiserfs_key *k1,
  151. const struct reiserfs_key *k2)
  152. {
  153. return memcmp(k1, k2, sizeof(struct reiserfs_key));
  154. }
  155. /**************************************************************************
  156. * Binary search toolkit function *
  157. * Search for an item in the array by the item key *
  158. * Returns: 1 if found, 0 if not found; *
  159. * *p_n_pos = number of the searched element if found, else the *
  160. * number of the first element that is larger than p_v_key. *
  161. **************************************************************************/
  162. /* For those not familiar with binary search: n_lbound is the leftmost item that it
  163. could be, n_rbound the rightmost item that it could be. We examine the item
  164. halfway between n_lbound and n_rbound, and that tells us either that we can increase
  165. n_lbound, or decrease n_rbound, or that we have found it, or if n_lbound <= n_rbound that
  166. there are no possible items, and we have not found it. With each examination we
  167. cut the number of possible items it could be by one more than half rounded down,
  168. or we find it. */
  169. static inline int bin_search(const void *p_v_key, /* Key to search for. */
  170. const void *p_v_base, /* First item in the array. */
  171. int p_n_num, /* Number of items in the array. */
  172. int p_n_width, /* Item size in the array.
  173. searched. Lest the reader be
  174. confused, note that this is crafted
  175. as a general function, and when it
  176. is applied specifically to the array
  177. of item headers in a node, p_n_width
  178. is actually the item header size not
  179. the item size. */
  180. int *p_n_pos /* Number of the searched for element. */
  181. )
  182. {
  183. int n_rbound, n_lbound, n_j;
  184. for (n_j = ((n_rbound = p_n_num - 1) + (n_lbound = 0)) / 2;
  185. n_lbound <= n_rbound; n_j = (n_rbound + n_lbound) / 2)
  186. switch (comp_keys
  187. ((struct reiserfs_key *)((char *)p_v_base +
  188. n_j * p_n_width),
  189. (struct cpu_key *)p_v_key)) {
  190. case -1:
  191. n_lbound = n_j + 1;
  192. continue;
  193. case 1:
  194. n_rbound = n_j - 1;
  195. continue;
  196. case 0:
  197. *p_n_pos = n_j;
  198. return ITEM_FOUND; /* Key found in the array. */
  199. }
  200. /* bin_search did not find given key, it returns position of key,
  201. that is minimal and greater than the given one. */
  202. *p_n_pos = n_lbound;
  203. return ITEM_NOT_FOUND;
  204. }
  205. #ifdef CONFIG_REISERFS_CHECK
  206. extern struct tree_balance *cur_tb;
  207. #endif
  208. /* Minimal possible key. It is never in the tree. */
  209. const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
  210. /* Maximal possible key. It is never in the tree. */
  211. static const struct reiserfs_key MAX_KEY = {
  212. __constant_cpu_to_le32(0xffffffff),
  213. __constant_cpu_to_le32(0xffffffff),
  214. {{__constant_cpu_to_le32(0xffffffff),
  215. __constant_cpu_to_le32(0xffffffff)},}
  216. };
  217. /* Get delimiting key of the buffer by looking for it in the buffers in the path, starting from the bottom
  218. of the path, and going upwards. We must check the path's validity at each step. If the key is not in
  219. the path, there is no delimiting key in the tree (buffer is first or last buffer in tree), and in this
  220. case we return a special key, either MIN_KEY or MAX_KEY. */
  221. static inline const struct reiserfs_key *get_lkey(const struct treepath
  222. *p_s_chk_path,
  223. const struct super_block
  224. *p_s_sb)
  225. {
  226. int n_position, n_path_offset = p_s_chk_path->path_length;
  227. struct buffer_head *p_s_parent;
  228. RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
  229. "PAP-5010: invalid offset in the path");
  230. /* While not higher in path than first element. */
  231. while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
  232. RFALSE(!buffer_uptodate
  233. (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)),
  234. "PAP-5020: parent is not uptodate");
  235. /* Parent at the path is not in the tree now. */
  236. if (!B_IS_IN_TREE
  237. (p_s_parent =
  238. PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)))
  239. return &MAX_KEY;
  240. /* Check whether position in the parent is correct. */
  241. if ((n_position =
  242. PATH_OFFSET_POSITION(p_s_chk_path,
  243. n_path_offset)) >
  244. B_NR_ITEMS(p_s_parent))
  245. return &MAX_KEY;
  246. /* Check whether parent at the path really points to the child. */
  247. if (B_N_CHILD_NUM(p_s_parent, n_position) !=
  248. PATH_OFFSET_PBUFFER(p_s_chk_path,
  249. n_path_offset + 1)->b_blocknr)
  250. return &MAX_KEY;
  251. /* Return delimiting key if position in the parent is not equal to zero. */
  252. if (n_position)
  253. return B_N_PDELIM_KEY(p_s_parent, n_position - 1);
  254. }
  255. /* Return MIN_KEY if we are in the root of the buffer tree. */
  256. if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)->
  257. b_blocknr == SB_ROOT_BLOCK(p_s_sb))
  258. return &MIN_KEY;
  259. return &MAX_KEY;
  260. }
  261. /* Get delimiting key of the buffer at the path and its right neighbor. */
  262. inline const struct reiserfs_key *get_rkey(const struct treepath *p_s_chk_path,
  263. const struct super_block *p_s_sb)
  264. {
  265. int n_position, n_path_offset = p_s_chk_path->path_length;
  266. struct buffer_head *p_s_parent;
  267. RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
  268. "PAP-5030: invalid offset in the path");
  269. while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
  270. RFALSE(!buffer_uptodate
  271. (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)),
  272. "PAP-5040: parent is not uptodate");
  273. /* Parent at the path is not in the tree now. */
  274. if (!B_IS_IN_TREE
  275. (p_s_parent =
  276. PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)))
  277. return &MIN_KEY;
  278. /* Check whether position in the parent is correct. */
  279. if ((n_position =
  280. PATH_OFFSET_POSITION(p_s_chk_path,
  281. n_path_offset)) >
  282. B_NR_ITEMS(p_s_parent))
  283. return &MIN_KEY;
  284. /* Check whether parent at the path really points to the child. */
  285. if (B_N_CHILD_NUM(p_s_parent, n_position) !=
  286. PATH_OFFSET_PBUFFER(p_s_chk_path,
  287. n_path_offset + 1)->b_blocknr)
  288. return &MIN_KEY;
  289. /* Return delimiting key if position in the parent is not the last one. */
  290. if (n_position != B_NR_ITEMS(p_s_parent))
  291. return B_N_PDELIM_KEY(p_s_parent, n_position);
  292. }
  293. /* Return MAX_KEY if we are in the root of the buffer tree. */
  294. if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)->
  295. b_blocknr == SB_ROOT_BLOCK(p_s_sb))
  296. return &MAX_KEY;
  297. return &MIN_KEY;
  298. }
  299. /* Check whether a key is contained in the tree rooted from a buffer at a path. */
  300. /* This works by looking at the left and right delimiting keys for the buffer in the last path_element in
  301. the path. These delimiting keys are stored at least one level above that buffer in the tree. If the
  302. buffer is the first or last node in the tree order then one of the delimiting keys may be absent, and in
  303. this case get_lkey and get_rkey return a special key which is MIN_KEY or MAX_KEY. */
  304. static inline int key_in_buffer(struct treepath *p_s_chk_path, /* Path which should be checked. */
  305. const struct cpu_key *p_s_key, /* Key which should be checked. */
  306. struct super_block *p_s_sb /* Super block pointer. */
  307. )
  308. {
  309. RFALSE(!p_s_key || p_s_chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
  310. || p_s_chk_path->path_length > MAX_HEIGHT,
  311. "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
  312. p_s_key, p_s_chk_path->path_length);
  313. RFALSE(!PATH_PLAST_BUFFER(p_s_chk_path)->b_bdev,
  314. "PAP-5060: device must not be NODEV");
  315. if (comp_keys(get_lkey(p_s_chk_path, p_s_sb), p_s_key) == 1)
  316. /* left delimiting key is bigger, that the key we look for */
  317. return 0;
  318. // if ( comp_keys(p_s_key, get_rkey(p_s_chk_path, p_s_sb)) != -1 )
  319. if (comp_keys(get_rkey(p_s_chk_path, p_s_sb), p_s_key) != 1)
  320. /* p_s_key must be less than right delimitiing key */
  321. return 0;
  322. return 1;
  323. }
  324. inline void decrement_bcount(struct buffer_head *p_s_bh)
  325. {
  326. if (p_s_bh) {
  327. if (atomic_read(&(p_s_bh->b_count))) {
  328. put_bh(p_s_bh);
  329. return;
  330. }
  331. reiserfs_panic(NULL,
  332. "PAP-5070: decrement_bcount: trying to free free buffer %b",
  333. p_s_bh);
  334. }
  335. }
  336. /* Decrement b_count field of the all buffers in the path. */
  337. void decrement_counters_in_path(struct treepath *p_s_search_path)
  338. {
  339. int n_path_offset = p_s_search_path->path_length;
  340. RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET ||
  341. n_path_offset > EXTENDED_MAX_HEIGHT - 1,
  342. "PAP-5080: invalid path offset of %d", n_path_offset);
  343. while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
  344. struct buffer_head *bh;
  345. bh = PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--);
  346. decrement_bcount(bh);
  347. }
  348. p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
  349. }
  350. int reiserfs_check_path(struct treepath *p)
  351. {
  352. RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
  353. "path not properly relsed");
  354. return 0;
  355. }
  356. /* Release all buffers in the path. Restore dirty bits clean
  357. ** when preparing the buffer for the log
  358. **
  359. ** only called from fix_nodes()
  360. */
  361. void pathrelse_and_restore(struct super_block *s, struct treepath *p_s_search_path)
  362. {
  363. int n_path_offset = p_s_search_path->path_length;
  364. RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
  365. "clm-4000: invalid path offset");
  366. while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
  367. reiserfs_restore_prepared_buffer(s,
  368. PATH_OFFSET_PBUFFER
  369. (p_s_search_path,
  370. n_path_offset));
  371. brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--));
  372. }
  373. p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
  374. }
  375. /* Release all buffers in the path. */
  376. void pathrelse(struct treepath *p_s_search_path)
  377. {
  378. int n_path_offset = p_s_search_path->path_length;
  379. RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
  380. "PAP-5090: invalid path offset");
  381. while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
  382. brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--));
  383. p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
  384. }
  385. static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
  386. {
  387. struct block_head *blkh;
  388. struct item_head *ih;
  389. int used_space;
  390. int prev_location;
  391. int i;
  392. int nr;
  393. blkh = (struct block_head *)buf;
  394. if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
  395. reiserfs_warning(NULL,
  396. "is_leaf: this should be caught earlier");
  397. return 0;
  398. }
  399. nr = blkh_nr_item(blkh);
  400. if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
  401. /* item number is too big or too small */
  402. reiserfs_warning(NULL, "is_leaf: nr_item seems wrong: %z", bh);
  403. return 0;
  404. }
  405. ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
  406. used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
  407. if (used_space != blocksize - blkh_free_space(blkh)) {
  408. /* free space does not match to calculated amount of use space */
  409. reiserfs_warning(NULL, "is_leaf: free space seems wrong: %z",
  410. bh);
  411. return 0;
  412. }
  413. // FIXME: it is_leaf will hit performance too much - we may have
  414. // return 1 here
  415. /* check tables of item heads */
  416. ih = (struct item_head *)(buf + BLKH_SIZE);
  417. prev_location = blocksize;
  418. for (i = 0; i < nr; i++, ih++) {
  419. if (le_ih_k_type(ih) == TYPE_ANY) {
  420. reiserfs_warning(NULL,
  421. "is_leaf: wrong item type for item %h",
  422. ih);
  423. return 0;
  424. }
  425. if (ih_location(ih) >= blocksize
  426. || ih_location(ih) < IH_SIZE * nr) {
  427. reiserfs_warning(NULL,
  428. "is_leaf: item location seems wrong: %h",
  429. ih);
  430. return 0;
  431. }
  432. if (ih_item_len(ih) < 1
  433. || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
  434. reiserfs_warning(NULL,
  435. "is_leaf: item length seems wrong: %h",
  436. ih);
  437. return 0;
  438. }
  439. if (prev_location - ih_location(ih) != ih_item_len(ih)) {
  440. reiserfs_warning(NULL,
  441. "is_leaf: item location seems wrong (second one): %h",
  442. ih);
  443. return 0;
  444. }
  445. prev_location = ih_location(ih);
  446. }
  447. // one may imagine much more checks
  448. return 1;
  449. }
  450. /* returns 1 if buf looks like an internal node, 0 otherwise */
  451. static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
  452. {
  453. struct block_head *blkh;
  454. int nr;
  455. int used_space;
  456. blkh = (struct block_head *)buf;
  457. nr = blkh_level(blkh);
  458. if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
  459. /* this level is not possible for internal nodes */
  460. reiserfs_warning(NULL,
  461. "is_internal: this should be caught earlier");
  462. return 0;
  463. }
  464. nr = blkh_nr_item(blkh);
  465. if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
  466. /* for internal which is not root we might check min number of keys */
  467. reiserfs_warning(NULL,
  468. "is_internal: number of key seems wrong: %z",
  469. bh);
  470. return 0;
  471. }
  472. used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
  473. if (used_space != blocksize - blkh_free_space(blkh)) {
  474. reiserfs_warning(NULL,
  475. "is_internal: free space seems wrong: %z", bh);
  476. return 0;
  477. }
  478. // one may imagine much more checks
  479. return 1;
  480. }
  481. // make sure that bh contains formatted node of reiserfs tree of
  482. // 'level'-th level
  483. static int is_tree_node(struct buffer_head *bh, int level)
  484. {
  485. if (B_LEVEL(bh) != level) {
  486. reiserfs_warning(NULL,
  487. "is_tree_node: node level %d does not match to the expected one %d",
  488. B_LEVEL(bh), level);
  489. return 0;
  490. }
  491. if (level == DISK_LEAF_NODE_LEVEL)
  492. return is_leaf(bh->b_data, bh->b_size, bh);
  493. return is_internal(bh->b_data, bh->b_size, bh);
  494. }
  495. #define SEARCH_BY_KEY_READA 16
  496. /* The function is NOT SCHEDULE-SAFE! */
  497. static void search_by_key_reada(struct super_block *s,
  498. struct buffer_head **bh,
  499. unsigned long *b, int num)
  500. {
  501. int i, j;
  502. for (i = 0; i < num; i++) {
  503. bh[i] = sb_getblk(s, b[i]);
  504. }
  505. for (j = 0; j < i; j++) {
  506. /*
  507. * note, this needs attention if we are getting rid of the BKL
  508. * you have to make sure the prepared bit isn't set on this buffer
  509. */
  510. if (!buffer_uptodate(bh[j]))
  511. ll_rw_block(READA, 1, bh + j);
  512. brelse(bh[j]);
  513. }
  514. }
  515. /**************************************************************************
  516. * Algorithm SearchByKey *
  517. * look for item in the Disk S+Tree by its key *
  518. * Input: p_s_sb - super block *
  519. * p_s_key - pointer to the key to search *
  520. * Output: ITEM_FOUND, ITEM_NOT_FOUND or IO_ERROR *
  521. * p_s_search_path - path from the root to the needed leaf *
  522. **************************************************************************/
  523. /* This function fills up the path from the root to the leaf as it
  524. descends the tree looking for the key. It uses reiserfs_bread to
  525. try to find buffers in the cache given their block number. If it
  526. does not find them in the cache it reads them from disk. For each
  527. node search_by_key finds using reiserfs_bread it then uses
  528. bin_search to look through that node. bin_search will find the
  529. position of the block_number of the next node if it is looking
  530. through an internal node. If it is looking through a leaf node
  531. bin_search will find the position of the item which has key either
  532. equal to given key, or which is the maximal key less than the given
  533. key. search_by_key returns a path that must be checked for the
  534. correctness of the top of the path but need not be checked for the
  535. correctness of the bottom of the path */
  536. /* The function is NOT SCHEDULE-SAFE! */
  537. int search_by_key(struct super_block *p_s_sb, const struct cpu_key *p_s_key, /* Key to search. */
  538. struct treepath *p_s_search_path,/* This structure was
  539. allocated and initialized
  540. by the calling
  541. function. It is filled up
  542. by this function. */
  543. int n_stop_level /* How far down the tree to search. To
  544. stop at leaf level - set to
  545. DISK_LEAF_NODE_LEVEL */
  546. )
  547. {
  548. int n_block_number;
  549. int expected_level;
  550. struct buffer_head *p_s_bh;
  551. struct path_element *p_s_last_element;
  552. int n_node_level, n_retval;
  553. int right_neighbor_of_leaf_node;
  554. int fs_gen;
  555. struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
  556. unsigned long reada_blocks[SEARCH_BY_KEY_READA];
  557. int reada_count = 0;
  558. #ifdef CONFIG_REISERFS_CHECK
  559. int n_repeat_counter = 0;
  560. #endif
  561. PROC_INFO_INC(p_s_sb, search_by_key);
  562. /* As we add each node to a path we increase its count. This means that
  563. we must be careful to release all nodes in a path before we either
  564. discard the path struct or re-use the path struct, as we do here. */
  565. decrement_counters_in_path(p_s_search_path);
  566. right_neighbor_of_leaf_node = 0;
  567. /* With each iteration of this loop we search through the items in the
  568. current node, and calculate the next current node(next path element)
  569. for the next iteration of this loop.. */
  570. n_block_number = SB_ROOT_BLOCK(p_s_sb);
  571. expected_level = -1;
  572. while (1) {
  573. #ifdef CONFIG_REISERFS_CHECK
  574. if (!(++n_repeat_counter % 50000))
  575. reiserfs_warning(p_s_sb, "PAP-5100: search_by_key: %s:"
  576. "there were %d iterations of while loop "
  577. "looking for key %K",
  578. current->comm, n_repeat_counter,
  579. p_s_key);
  580. #endif
  581. /* prep path to have another element added to it. */
  582. p_s_last_element =
  583. PATH_OFFSET_PELEMENT(p_s_search_path,
  584. ++p_s_search_path->path_length);
  585. fs_gen = get_generation(p_s_sb);
  586. /* Read the next tree node, and set the last element in the path to
  587. have a pointer to it. */
  588. if ((p_s_bh = p_s_last_element->pe_buffer =
  589. sb_getblk(p_s_sb, n_block_number))) {
  590. if (!buffer_uptodate(p_s_bh) && reada_count > 1) {
  591. search_by_key_reada(p_s_sb, reada_bh,
  592. reada_blocks, reada_count);
  593. }
  594. ll_rw_block(READ, 1, &p_s_bh);
  595. wait_on_buffer(p_s_bh);
  596. if (!buffer_uptodate(p_s_bh))
  597. goto io_error;
  598. } else {
  599. io_error:
  600. p_s_search_path->path_length--;
  601. pathrelse(p_s_search_path);
  602. return IO_ERROR;
  603. }
  604. reada_count = 0;
  605. if (expected_level == -1)
  606. expected_level = SB_TREE_HEIGHT(p_s_sb);
  607. expected_level--;
  608. /* It is possible that schedule occurred. We must check whether the key
  609. to search is still in the tree rooted from the current buffer. If
  610. not then repeat search from the root. */
  611. if (fs_changed(fs_gen, p_s_sb) &&
  612. (!B_IS_IN_TREE(p_s_bh) ||
  613. B_LEVEL(p_s_bh) != expected_level ||
  614. !key_in_buffer(p_s_search_path, p_s_key, p_s_sb))) {
  615. PROC_INFO_INC(p_s_sb, search_by_key_fs_changed);
  616. PROC_INFO_INC(p_s_sb, search_by_key_restarted);
  617. PROC_INFO_INC(p_s_sb,
  618. sbk_restarted[expected_level - 1]);
  619. decrement_counters_in_path(p_s_search_path);
  620. /* Get the root block number so that we can repeat the search
  621. starting from the root. */
  622. n_block_number = SB_ROOT_BLOCK(p_s_sb);
  623. expected_level = -1;
  624. right_neighbor_of_leaf_node = 0;
  625. /* repeat search from the root */
  626. continue;
  627. }
  628. /* only check that the key is in the buffer if p_s_key is not
  629. equal to the MAX_KEY. Latter case is only possible in
  630. "finish_unfinished()" processing during mount. */
  631. RFALSE(comp_keys(&MAX_KEY, p_s_key) &&
  632. !key_in_buffer(p_s_search_path, p_s_key, p_s_sb),
  633. "PAP-5130: key is not in the buffer");
  634. #ifdef CONFIG_REISERFS_CHECK
  635. if (cur_tb) {
  636. print_cur_tb("5140");
  637. reiserfs_panic(p_s_sb,
  638. "PAP-5140: search_by_key: schedule occurred in do_balance!");
  639. }
  640. #endif
  641. // make sure, that the node contents look like a node of
  642. // certain level
  643. if (!is_tree_node(p_s_bh, expected_level)) {
  644. reiserfs_warning(p_s_sb, "vs-5150: search_by_key: "
  645. "invalid format found in block %ld. Fsck?",
  646. p_s_bh->b_blocknr);
  647. pathrelse(p_s_search_path);
  648. return IO_ERROR;
  649. }
  650. /* ok, we have acquired next formatted node in the tree */
  651. n_node_level = B_LEVEL(p_s_bh);
  652. PROC_INFO_BH_STAT(p_s_sb, p_s_bh, n_node_level - 1);
  653. RFALSE(n_node_level < n_stop_level,
  654. "vs-5152: tree level (%d) is less than stop level (%d)",
  655. n_node_level, n_stop_level);
  656. n_retval = bin_search(p_s_key, B_N_PITEM_HEAD(p_s_bh, 0),
  657. B_NR_ITEMS(p_s_bh),
  658. (n_node_level ==
  659. DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
  660. KEY_SIZE,
  661. &(p_s_last_element->pe_position));
  662. if (n_node_level == n_stop_level) {
  663. return n_retval;
  664. }
  665. /* we are not in the stop level */
  666. if (n_retval == ITEM_FOUND)
  667. /* item has been found, so we choose the pointer which is to the right of the found one */
  668. p_s_last_element->pe_position++;
  669. /* if item was not found we choose the position which is to
  670. the left of the found item. This requires no code,
  671. bin_search did it already. */
  672. /* So we have chosen a position in the current node which is
  673. an internal node. Now we calculate child block number by
  674. position in the node. */
  675. n_block_number =
  676. B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position);
  677. /* if we are going to read leaf nodes, try for read ahead as well */
  678. if ((p_s_search_path->reada & PATH_READA) &&
  679. n_node_level == DISK_LEAF_NODE_LEVEL + 1) {
  680. int pos = p_s_last_element->pe_position;
  681. int limit = B_NR_ITEMS(p_s_bh);
  682. struct reiserfs_key *le_key;
  683. if (p_s_search_path->reada & PATH_READA_BACK)
  684. limit = 0;
  685. while (reada_count < SEARCH_BY_KEY_READA) {
  686. if (pos == limit)
  687. break;
  688. reada_blocks[reada_count++] =
  689. B_N_CHILD_NUM(p_s_bh, pos);
  690. if (p_s_search_path->reada & PATH_READA_BACK)
  691. pos--;
  692. else
  693. pos++;
  694. /*
  695. * check to make sure we're in the same object
  696. */
  697. le_key = B_N_PDELIM_KEY(p_s_bh, pos);
  698. if (le32_to_cpu(le_key->k_objectid) !=
  699. p_s_key->on_disk_key.k_objectid) {
  700. break;
  701. }
  702. }
  703. }
  704. }
  705. }
  706. /* Form the path to an item and position in this item which contains
  707. file byte defined by p_s_key. If there is no such item
  708. corresponding to the key, we point the path to the item with
  709. maximal key less than p_s_key, and *p_n_pos_in_item is set to one
  710. past the last entry/byte in the item. If searching for entry in a
  711. directory item, and it is not found, *p_n_pos_in_item is set to one
  712. entry more than the entry with maximal key which is less than the
  713. sought key.
  714. Note that if there is no entry in this same node which is one more,
  715. then we point to an imaginary entry. for direct items, the
  716. position is in units of bytes, for indirect items the position is
  717. in units of blocknr entries, for directory items the position is in
  718. units of directory entries. */
  719. /* The function is NOT SCHEDULE-SAFE! */
  720. int search_for_position_by_key(struct super_block *p_s_sb, /* Pointer to the super block. */
  721. const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */
  722. struct treepath *p_s_search_path /* Filled up by this function. */
  723. )
  724. {
  725. struct item_head *p_le_ih; /* pointer to on-disk structure */
  726. int n_blk_size;
  727. loff_t item_offset, offset;
  728. struct reiserfs_dir_entry de;
  729. int retval;
  730. /* If searching for directory entry. */
  731. if (is_direntry_cpu_key(p_cpu_key))
  732. return search_by_entry_key(p_s_sb, p_cpu_key, p_s_search_path,
  733. &de);
  734. /* If not searching for directory entry. */
  735. /* If item is found. */
  736. retval = search_item(p_s_sb, p_cpu_key, p_s_search_path);
  737. if (retval == IO_ERROR)
  738. return retval;
  739. if (retval == ITEM_FOUND) {
  740. RFALSE(!ih_item_len
  741. (B_N_PITEM_HEAD
  742. (PATH_PLAST_BUFFER(p_s_search_path),
  743. PATH_LAST_POSITION(p_s_search_path))),
  744. "PAP-5165: item length equals zero");
  745. pos_in_item(p_s_search_path) = 0;
  746. return POSITION_FOUND;
  747. }
  748. RFALSE(!PATH_LAST_POSITION(p_s_search_path),
  749. "PAP-5170: position equals zero");
  750. /* Item is not found. Set path to the previous item. */
  751. p_le_ih =
  752. B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_search_path),
  753. --PATH_LAST_POSITION(p_s_search_path));
  754. n_blk_size = p_s_sb->s_blocksize;
  755. if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
  756. return FILE_NOT_FOUND;
  757. }
  758. // FIXME: quite ugly this far
  759. item_offset = le_ih_k_offset(p_le_ih);
  760. offset = cpu_key_k_offset(p_cpu_key);
  761. /* Needed byte is contained in the item pointed to by the path. */
  762. if (item_offset <= offset &&
  763. item_offset + op_bytes_number(p_le_ih, n_blk_size) > offset) {
  764. pos_in_item(p_s_search_path) = offset - item_offset;
  765. if (is_indirect_le_ih(p_le_ih)) {
  766. pos_in_item(p_s_search_path) /= n_blk_size;
  767. }
  768. return POSITION_FOUND;
  769. }
  770. /* Needed byte is not contained in the item pointed to by the
  771. path. Set pos_in_item out of the item. */
  772. if (is_indirect_le_ih(p_le_ih))
  773. pos_in_item(p_s_search_path) =
  774. ih_item_len(p_le_ih) / UNFM_P_SIZE;
  775. else
  776. pos_in_item(p_s_search_path) = ih_item_len(p_le_ih);
  777. return POSITION_NOT_FOUND;
  778. }
  779. /* Compare given item and item pointed to by the path. */
  780. int comp_items(const struct item_head *stored_ih, const struct treepath *p_s_path)
  781. {
  782. struct buffer_head *p_s_bh;
  783. struct item_head *ih;
  784. /* Last buffer at the path is not in the tree. */
  785. if (!B_IS_IN_TREE(p_s_bh = PATH_PLAST_BUFFER(p_s_path)))
  786. return 1;
  787. /* Last path position is invalid. */
  788. if (PATH_LAST_POSITION(p_s_path) >= B_NR_ITEMS(p_s_bh))
  789. return 1;
  790. /* we need only to know, whether it is the same item */
  791. ih = get_ih(p_s_path);
  792. return memcmp(stored_ih, ih, IH_SIZE);
  793. }
  794. /* unformatted nodes are not logged anymore, ever. This is safe
  795. ** now
  796. */
  797. #define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
  798. // block can not be forgotten as it is in I/O or held by someone
  799. #define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
  800. // prepare for delete or cut of direct item
  801. static inline int prepare_for_direct_item(struct treepath *path,
  802. struct item_head *le_ih,
  803. struct inode *inode,
  804. loff_t new_file_length, int *cut_size)
  805. {
  806. loff_t round_len;
  807. if (new_file_length == max_reiserfs_offset(inode)) {
  808. /* item has to be deleted */
  809. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  810. return M_DELETE;
  811. }
  812. // new file gets truncated
  813. if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
  814. //
  815. round_len = ROUND_UP(new_file_length);
  816. /* this was n_new_file_length < le_ih ... */
  817. if (round_len < le_ih_k_offset(le_ih)) {
  818. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  819. return M_DELETE; /* Delete this item. */
  820. }
  821. /* Calculate first position and size for cutting from item. */
  822. pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
  823. *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
  824. return M_CUT; /* Cut from this item. */
  825. }
  826. // old file: items may have any length
  827. if (new_file_length < le_ih_k_offset(le_ih)) {
  828. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  829. return M_DELETE; /* Delete this item. */
  830. }
  831. /* Calculate first position and size for cutting from item. */
  832. *cut_size = -(ih_item_len(le_ih) -
  833. (pos_in_item(path) =
  834. new_file_length + 1 - le_ih_k_offset(le_ih)));
  835. return M_CUT; /* Cut from this item. */
  836. }
  837. static inline int prepare_for_direntry_item(struct treepath *path,
  838. struct item_head *le_ih,
  839. struct inode *inode,
  840. loff_t new_file_length,
  841. int *cut_size)
  842. {
  843. if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
  844. new_file_length == max_reiserfs_offset(inode)) {
  845. RFALSE(ih_entry_count(le_ih) != 2,
  846. "PAP-5220: incorrect empty directory item (%h)", le_ih);
  847. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  848. return M_DELETE; /* Delete the directory item containing "." and ".." entry. */
  849. }
  850. if (ih_entry_count(le_ih) == 1) {
  851. /* Delete the directory item such as there is one record only
  852. in this item */
  853. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  854. return M_DELETE;
  855. }
  856. /* Cut one record from the directory item. */
  857. *cut_size =
  858. -(DEH_SIZE +
  859. entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
  860. return M_CUT;
  861. }
  862. #define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
  863. /* If the path points to a directory or direct item, calculate mode and the size cut, for balance.
  864. If the path points to an indirect item, remove some number of its unformatted nodes.
  865. In case of file truncate calculate whether this item must be deleted/truncated or last
  866. unformatted node of this item will be converted to a direct item.
  867. This function returns a determination of what balance mode the calling function should employ. */
  868. static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct treepath *p_s_path, const struct cpu_key *p_s_item_key, int *p_n_removed, /* Number of unformatted nodes which were removed
  869. from end of the file. */
  870. int *p_n_cut_size, unsigned long long n_new_file_length /* MAX_KEY_OFFSET in case of delete. */
  871. )
  872. {
  873. struct super_block *p_s_sb = inode->i_sb;
  874. struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_path);
  875. struct buffer_head *p_s_bh = PATH_PLAST_BUFFER(p_s_path);
  876. BUG_ON(!th->t_trans_id);
  877. /* Stat_data item. */
  878. if (is_statdata_le_ih(p_le_ih)) {
  879. RFALSE(n_new_file_length != max_reiserfs_offset(inode),
  880. "PAP-5210: mode must be M_DELETE");
  881. *p_n_cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
  882. return M_DELETE;
  883. }
  884. /* Directory item. */
  885. if (is_direntry_le_ih(p_le_ih))
  886. return prepare_for_direntry_item(p_s_path, p_le_ih, inode,
  887. n_new_file_length,
  888. p_n_cut_size);
  889. /* Direct item. */
  890. if (is_direct_le_ih(p_le_ih))
  891. return prepare_for_direct_item(p_s_path, p_le_ih, inode,
  892. n_new_file_length, p_n_cut_size);
  893. /* Case of an indirect item. */
  894. {
  895. int blk_size = p_s_sb->s_blocksize;
  896. struct item_head s_ih;
  897. int need_re_search;
  898. int delete = 0;
  899. int result = M_CUT;
  900. int pos = 0;
  901. if ( n_new_file_length == max_reiserfs_offset (inode) ) {
  902. /* prepare_for_delete_or_cut() is called by
  903. * reiserfs_delete_item() */
  904. n_new_file_length = 0;
  905. delete = 1;
  906. }
  907. do {
  908. need_re_search = 0;
  909. *p_n_cut_size = 0;
  910. p_s_bh = PATH_PLAST_BUFFER(p_s_path);
  911. copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
  912. pos = I_UNFM_NUM(&s_ih);
  913. while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > n_new_file_length) {
  914. __u32 *unfm, block;
  915. /* Each unformatted block deletion may involve one additional
  916. * bitmap block into the transaction, thereby the initial
  917. * journal space reservation might not be enough. */
  918. if (!delete && (*p_n_cut_size) != 0 &&
  919. reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
  920. break;
  921. }
  922. unfm = (__u32 *)B_I_PITEM(p_s_bh, &s_ih) + pos - 1;
  923. block = get_block_num(unfm, 0);
  924. if (block != 0) {
  925. reiserfs_prepare_for_journal(p_s_sb, p_s_bh, 1);
  926. put_block_num(unfm, 0, 0);
  927. journal_mark_dirty (th, p_s_sb, p_s_bh);
  928. reiserfs_free_block(th, inode, block, 1);
  929. }
  930. cond_resched();
  931. if (item_moved (&s_ih, p_s_path)) {
  932. need_re_search = 1;
  933. break;
  934. }
  935. pos --;
  936. (*p_n_removed) ++;
  937. (*p_n_cut_size) -= UNFM_P_SIZE;
  938. if (pos == 0) {
  939. (*p_n_cut_size) -= IH_SIZE;
  940. result = M_DELETE;
  941. break;
  942. }
  943. }
  944. /* a trick. If the buffer has been logged, this will do nothing. If
  945. ** we've broken the loop without logging it, it will restore the
  946. ** buffer */
  947. reiserfs_restore_prepared_buffer(p_s_sb, p_s_bh);
  948. } while (need_re_search &&
  949. search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path) == POSITION_FOUND);
  950. pos_in_item(p_s_path) = pos * UNFM_P_SIZE;
  951. if (*p_n_cut_size == 0) {
  952. /* Nothing were cut. maybe convert last unformatted node to the
  953. * direct item? */
  954. result = M_CONVERT;
  955. }
  956. return result;
  957. }
  958. }
  959. /* Calculate number of bytes which will be deleted or cut during balance */
  960. static int calc_deleted_bytes_number(struct tree_balance *p_s_tb, char c_mode)
  961. {
  962. int n_del_size;
  963. struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_tb->tb_path);
  964. if (is_statdata_le_ih(p_le_ih))
  965. return 0;
  966. n_del_size =
  967. (c_mode ==
  968. M_DELETE) ? ih_item_len(p_le_ih) : -p_s_tb->insert_size[0];
  969. if (is_direntry_le_ih(p_le_ih)) {
  970. // return EMPTY_DIR_SIZE; /* We delete emty directoris only. */
  971. // we can't use EMPTY_DIR_SIZE, as old format dirs have a different
  972. // empty size. ick. FIXME, is this right?
  973. //
  974. return n_del_size;
  975. }
  976. if (is_indirect_le_ih(p_le_ih))
  977. n_del_size = (n_del_size / UNFM_P_SIZE) * (PATH_PLAST_BUFFER(p_s_tb->tb_path)->b_size); // - get_ih_free_space (p_le_ih);
  978. return n_del_size;
  979. }
  980. static void init_tb_struct(struct reiserfs_transaction_handle *th,
  981. struct tree_balance *p_s_tb,
  982. struct super_block *p_s_sb,
  983. struct treepath *p_s_path, int n_size)
  984. {
  985. BUG_ON(!th->t_trans_id);
  986. memset(p_s_tb, '\0', sizeof(struct tree_balance));
  987. p_s_tb->transaction_handle = th;
  988. p_s_tb->tb_sb = p_s_sb;
  989. p_s_tb->tb_path = p_s_path;
  990. PATH_OFFSET_PBUFFER(p_s_path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
  991. PATH_OFFSET_POSITION(p_s_path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
  992. p_s_tb->insert_size[0] = n_size;
  993. }
  994. void padd_item(char *item, int total_length, int length)
  995. {
  996. int i;
  997. for (i = total_length; i > length;)
  998. item[--i] = 0;
  999. }
  1000. #ifdef REISERQUOTA_DEBUG
  1001. char key2type(struct reiserfs_key *ih)
  1002. {
  1003. if (is_direntry_le_key(2, ih))
  1004. return 'd';
  1005. if (is_direct_le_key(2, ih))
  1006. return 'D';
  1007. if (is_indirect_le_key(2, ih))
  1008. return 'i';
  1009. if (is_statdata_le_key(2, ih))
  1010. return 's';
  1011. return 'u';
  1012. }
  1013. char head2type(struct item_head *ih)
  1014. {
  1015. if (is_direntry_le_ih(ih))
  1016. return 'd';
  1017. if (is_direct_le_ih(ih))
  1018. return 'D';
  1019. if (is_indirect_le_ih(ih))
  1020. return 'i';
  1021. if (is_statdata_le_ih(ih))
  1022. return 's';
  1023. return 'u';
  1024. }
  1025. #endif
  1026. /* Delete object item. */
  1027. int reiserfs_delete_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_path, /* Path to the deleted item. */
  1028. const struct cpu_key *p_s_item_key, /* Key to search for the deleted item. */
  1029. struct inode *p_s_inode, /* inode is here just to update i_blocks and quotas */
  1030. struct buffer_head *p_s_un_bh)
  1031. { /* NULL or unformatted node pointer. */
  1032. struct super_block *p_s_sb = p_s_inode->i_sb;
  1033. struct tree_balance s_del_balance;
  1034. struct item_head s_ih;
  1035. struct item_head *q_ih;
  1036. int quota_cut_bytes;
  1037. int n_ret_value, n_del_size, n_removed;
  1038. #ifdef CONFIG_REISERFS_CHECK
  1039. char c_mode;
  1040. int n_iter = 0;
  1041. #endif
  1042. BUG_ON(!th->t_trans_id);
  1043. init_tb_struct(th, &s_del_balance, p_s_sb, p_s_path,
  1044. 0 /*size is unknown */ );
  1045. while (1) {
  1046. n_removed = 0;
  1047. #ifdef CONFIG_REISERFS_CHECK
  1048. n_iter++;
  1049. c_mode =
  1050. #endif
  1051. prepare_for_delete_or_cut(th, p_s_inode, p_s_path,
  1052. p_s_item_key, &n_removed,
  1053. &n_del_size,
  1054. max_reiserfs_offset(p_s_inode));
  1055. RFALSE(c_mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
  1056. copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
  1057. s_del_balance.insert_size[0] = n_del_size;
  1058. n_ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
  1059. if (n_ret_value != REPEAT_SEARCH)
  1060. break;
  1061. PROC_INFO_INC(p_s_sb, delete_item_restarted);
  1062. // file system changed, repeat search
  1063. n_ret_value =
  1064. search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path);
  1065. if (n_ret_value == IO_ERROR)
  1066. break;
  1067. if (n_ret_value == FILE_NOT_FOUND) {
  1068. reiserfs_warning(p_s_sb,
  1069. "vs-5340: reiserfs_delete_item: "
  1070. "no items of the file %K found",
  1071. p_s_item_key);
  1072. break;
  1073. }
  1074. } /* while (1) */
  1075. if (n_ret_value != CARRY_ON) {
  1076. unfix_nodes(&s_del_balance);
  1077. return 0;
  1078. }
  1079. // reiserfs_delete_item returns item length when success
  1080. n_ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
  1081. q_ih = get_ih(p_s_path);
  1082. quota_cut_bytes = ih_item_len(q_ih);
  1083. /* hack so the quota code doesn't have to guess if the file
  1084. ** has a tail. On tail insert, we allocate quota for 1 unformatted node.
  1085. ** We test the offset because the tail might have been
  1086. ** split into multiple items, and we only want to decrement for
  1087. ** the unfm node once
  1088. */
  1089. if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(q_ih)) {
  1090. if ((le_ih_k_offset(q_ih) & (p_s_sb->s_blocksize - 1)) == 1) {
  1091. quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE;
  1092. } else {
  1093. quota_cut_bytes = 0;
  1094. }
  1095. }
  1096. if (p_s_un_bh) {
  1097. int off;
  1098. char *data;
  1099. /* We are in direct2indirect conversion, so move tail contents
  1100. to the unformatted node */
  1101. /* note, we do the copy before preparing the buffer because we
  1102. ** don't care about the contents of the unformatted node yet.
  1103. ** the only thing we really care about is the direct item's data
  1104. ** is in the unformatted node.
  1105. **
  1106. ** Otherwise, we would have to call reiserfs_prepare_for_journal on
  1107. ** the unformatted node, which might schedule, meaning we'd have to
  1108. ** loop all the way back up to the start of the while loop.
  1109. **
  1110. ** The unformatted node must be dirtied later on. We can't be
  1111. ** sure here if the entire tail has been deleted yet.
  1112. **
  1113. ** p_s_un_bh is from the page cache (all unformatted nodes are
  1114. ** from the page cache) and might be a highmem page. So, we
  1115. ** can't use p_s_un_bh->b_data.
  1116. ** -clm
  1117. */
  1118. data = kmap_atomic(p_s_un_bh->b_page, KM_USER0);
  1119. off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1));
  1120. memcpy(data + off,
  1121. B_I_PITEM(PATH_PLAST_BUFFER(p_s_path), &s_ih),
  1122. n_ret_value);
  1123. kunmap_atomic(data, KM_USER0);
  1124. }
  1125. /* Perform balancing after all resources have been collected at once. */
  1126. do_balance(&s_del_balance, NULL, NULL, M_DELETE);
  1127. #ifdef REISERQUOTA_DEBUG
  1128. reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
  1129. "reiserquota delete_item(): freeing %u, id=%u type=%c",
  1130. quota_cut_bytes, p_s_inode->i_uid, head2type(&s_ih));
  1131. #endif
  1132. DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes);
  1133. /* Return deleted body length */
  1134. return n_ret_value;
  1135. }
  1136. /* Summary Of Mechanisms For Handling Collisions Between Processes:
  1137. deletion of the body of the object is performed by iput(), with the
  1138. result that if multiple processes are operating on a file, the
  1139. deletion of the body of the file is deferred until the last process
  1140. that has an open inode performs its iput().
  1141. writes and truncates are protected from collisions by use of
  1142. semaphores.
  1143. creates, linking, and mknod are protected from collisions with other
  1144. processes by making the reiserfs_add_entry() the last step in the
  1145. creation, and then rolling back all changes if there was a collision.
  1146. - Hans
  1147. */
  1148. /* this deletes item which never gets split */
  1149. void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
  1150. struct inode *inode, struct reiserfs_key *key)
  1151. {
  1152. struct tree_balance tb;
  1153. INITIALIZE_PATH(path);
  1154. int item_len = 0;
  1155. int tb_init = 0;
  1156. struct cpu_key cpu_key;
  1157. int retval;
  1158. int quota_cut_bytes = 0;
  1159. BUG_ON(!th->t_trans_id);
  1160. le_key2cpu_key(&cpu_key, key);
  1161. while (1) {
  1162. retval = search_item(th->t_super, &cpu_key, &path);
  1163. if (retval == IO_ERROR) {
  1164. reiserfs_warning(th->t_super,
  1165. "vs-5350: reiserfs_delete_solid_item: "
  1166. "i/o failure occurred trying to delete %K",
  1167. &cpu_key);
  1168. break;
  1169. }
  1170. if (retval != ITEM_FOUND) {
  1171. pathrelse(&path);
  1172. // No need for a warning, if there is just no free space to insert '..' item into the newly-created subdir
  1173. if (!
  1174. ((unsigned long long)
  1175. GET_HASH_VALUE(le_key_k_offset
  1176. (le_key_version(key), key)) == 0
  1177. && (unsigned long long)
  1178. GET_GENERATION_NUMBER(le_key_k_offset
  1179. (le_key_version(key),
  1180. key)) == 1))
  1181. reiserfs_warning(th->t_super,
  1182. "vs-5355: reiserfs_delete_solid_item: %k not found",
  1183. key);
  1184. break;
  1185. }
  1186. if (!tb_init) {
  1187. tb_init = 1;
  1188. item_len = ih_item_len(PATH_PITEM_HEAD(&path));
  1189. init_tb_struct(th, &tb, th->t_super, &path,
  1190. -(IH_SIZE + item_len));
  1191. }
  1192. quota_cut_bytes = ih_item_len(PATH_PITEM_HEAD(&path));
  1193. retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
  1194. if (retval == REPEAT_SEARCH) {
  1195. PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
  1196. continue;
  1197. }
  1198. if (retval == CARRY_ON) {
  1199. do_balance(&tb, NULL, NULL, M_DELETE);
  1200. if (inode) { /* Should we count quota for item? (we don't count quotas for save-links) */
  1201. #ifdef REISERQUOTA_DEBUG
  1202. reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
  1203. "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
  1204. quota_cut_bytes, inode->i_uid,
  1205. key2type(key));
  1206. #endif
  1207. DQUOT_FREE_SPACE_NODIRTY(inode,
  1208. quota_cut_bytes);
  1209. }
  1210. break;
  1211. }
  1212. // IO_ERROR, NO_DISK_SPACE, etc
  1213. reiserfs_warning(th->t_super,
  1214. "vs-5360: reiserfs_delete_solid_item: "
  1215. "could not delete %K due to fix_nodes failure",
  1216. &cpu_key);
  1217. unfix_nodes(&tb);
  1218. break;
  1219. }
  1220. reiserfs_check_path(&path);
  1221. }
  1222. int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
  1223. struct inode *inode)
  1224. {
  1225. int err;
  1226. inode->i_size = 0;
  1227. BUG_ON(!th->t_trans_id);
  1228. /* for directory this deletes item containing "." and ".." */
  1229. err =
  1230. reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ );
  1231. if (err)
  1232. return err;
  1233. #if defined( USE_INODE_GENERATION_COUNTER )
  1234. if (!old_format_only(th->t_super)) {
  1235. __le32 *inode_generation;
  1236. inode_generation =
  1237. &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
  1238. *inode_generation =
  1239. cpu_to_le32(le32_to_cpu(*inode_generation) + 1);
  1240. }
  1241. /* USE_INODE_GENERATION_COUNTER */
  1242. #endif
  1243. reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
  1244. return err;
  1245. }
  1246. static void unmap_buffers(struct page *page, loff_t pos)
  1247. {
  1248. struct buffer_head *bh;
  1249. struct buffer_head *head;
  1250. struct buffer_head *next;
  1251. unsigned long tail_index;
  1252. unsigned long cur_index;
  1253. if (page) {
  1254. if (page_has_buffers(page)) {
  1255. tail_index = pos & (PAGE_CACHE_SIZE - 1);
  1256. cur_index = 0;
  1257. head = page_buffers(page);
  1258. bh = head;
  1259. do {
  1260. next = bh->b_this_page;
  1261. /* we want to unmap the buffers that contain the tail, and
  1262. ** all the buffers after it (since the tail must be at the
  1263. ** end of the file). We don't want to unmap file data
  1264. ** before the tail, since it might be dirty and waiting to
  1265. ** reach disk
  1266. */
  1267. cur_index += bh->b_size;
  1268. if (cur_index > tail_index) {
  1269. reiserfs_unmap_buffer(bh);
  1270. }
  1271. bh = next;
  1272. } while (bh != head);
  1273. if (PAGE_SIZE == bh->b_size) {
  1274. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1275. }
  1276. }
  1277. }
  1278. }
  1279. static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
  1280. struct inode *p_s_inode,
  1281. struct page *page,
  1282. struct treepath *p_s_path,
  1283. const struct cpu_key *p_s_item_key,
  1284. loff_t n_new_file_size, char *p_c_mode)
  1285. {
  1286. struct super_block *p_s_sb = p_s_inode->i_sb;
  1287. int n_block_size = p_s_sb->s_blocksize;
  1288. int cut_bytes;
  1289. BUG_ON(!th->t_trans_id);
  1290. BUG_ON(n_new_file_size != p_s_inode->i_size);
  1291. /* the page being sent in could be NULL if there was an i/o error
  1292. ** reading in the last block. The user will hit problems trying to
  1293. ** read the file, but for now we just skip the indirect2direct
  1294. */
  1295. if (atomic_read(&p_s_inode->i_count) > 1 ||
  1296. !tail_has_to_be_packed(p_s_inode) ||
  1297. !page || (REISERFS_I(p_s_inode)->i_flags & i_nopack_mask)) {
  1298. // leave tail in an unformatted node
  1299. *p_c_mode = M_SKIP_BALANCING;
  1300. cut_bytes =
  1301. n_block_size - (n_new_file_size & (n_block_size - 1));
  1302. pathrelse(p_s_path);
  1303. return cut_bytes;
  1304. }
  1305. /* Permorm the conversion to a direct_item. */
  1306. /*return indirect_to_direct (p_s_inode, p_s_path, p_s_item_key, n_new_file_size, p_c_mode); */
  1307. return indirect2direct(th, p_s_inode, page, p_s_path, p_s_item_key,
  1308. n_new_file_size, p_c_mode);
  1309. }
  1310. /* we did indirect_to_direct conversion. And we have inserted direct
  1311. item successesfully, but there were no disk space to cut unfm
  1312. pointer being converted. Therefore we have to delete inserted
  1313. direct item(s) */
  1314. static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
  1315. struct inode *inode, struct treepath *path)
  1316. {
  1317. struct cpu_key tail_key;
  1318. int tail_len;
  1319. int removed;
  1320. BUG_ON(!th->t_trans_id);
  1321. make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4); // !!!!
  1322. tail_key.key_length = 4;
  1323. tail_len =
  1324. (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
  1325. while (tail_len) {
  1326. /* look for the last byte of the tail */
  1327. if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
  1328. POSITION_NOT_FOUND)
  1329. reiserfs_panic(inode->i_sb,
  1330. "vs-5615: indirect_to_direct_roll_back: found invalid item");
  1331. RFALSE(path->pos_in_item !=
  1332. ih_item_len(PATH_PITEM_HEAD(path)) - 1,
  1333. "vs-5616: appended bytes found");
  1334. PATH_LAST_POSITION(path)--;
  1335. removed =
  1336. reiserfs_delete_item(th, path, &tail_key, inode,
  1337. NULL /*unbh not needed */ );
  1338. RFALSE(removed <= 0
  1339. || removed > tail_len,
  1340. "vs-5617: there was tail %d bytes, removed item length %d bytes",
  1341. tail_len, removed);
  1342. tail_len -= removed;
  1343. set_cpu_key_k_offset(&tail_key,
  1344. cpu_key_k_offset(&tail_key) - removed);
  1345. }
  1346. reiserfs_warning(inode->i_sb,
  1347. "indirect_to_direct_roll_back: indirect_to_direct conversion has been rolled back due to lack of disk space");
  1348. //mark_file_without_tail (inode);
  1349. mark_inode_dirty(inode);
  1350. }
  1351. /* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
  1352. int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
  1353. struct treepath *p_s_path,
  1354. struct cpu_key *p_s_item_key,
  1355. struct inode *p_s_inode,
  1356. struct page *page, loff_t n_new_file_size)
  1357. {
  1358. struct super_block *p_s_sb = p_s_inode->i_sb;
  1359. /* Every function which is going to call do_balance must first
  1360. create a tree_balance structure. Then it must fill up this
  1361. structure by using the init_tb_struct and fix_nodes functions.
  1362. After that we can make tree balancing. */
  1363. struct tree_balance s_cut_balance;
  1364. struct item_head *p_le_ih;
  1365. int n_cut_size = 0, /* Amount to be cut. */
  1366. n_ret_value = CARRY_ON, n_removed = 0, /* Number of the removed unformatted nodes. */
  1367. n_is_inode_locked = 0;
  1368. char c_mode; /* Mode of the balance. */
  1369. int retval2 = -1;
  1370. int quota_cut_bytes;
  1371. loff_t tail_pos = 0;
  1372. BUG_ON(!th->t_trans_id);
  1373. init_tb_struct(th, &s_cut_balance, p_s_inode->i_sb, p_s_path,
  1374. n_cut_size);
  1375. /* Repeat this loop until we either cut the item without needing
  1376. to balance, or we fix_nodes without schedule occurring */
  1377. while (1) {
  1378. /* Determine the balance mode, position of the first byte to
  1379. be cut, and size to be cut. In case of the indirect item
  1380. free unformatted nodes which are pointed to by the cut
  1381. pointers. */
  1382. c_mode =
  1383. prepare_for_delete_or_cut(th, p_s_inode, p_s_path,
  1384. p_s_item_key, &n_removed,
  1385. &n_cut_size, n_new_file_size);
  1386. if (c_mode == M_CONVERT) {
  1387. /* convert last unformatted node to direct item or leave
  1388. tail in the unformatted node */
  1389. RFALSE(n_ret_value != CARRY_ON,
  1390. "PAP-5570: can not convert twice");
  1391. n_ret_value =
  1392. maybe_indirect_to_direct(th, p_s_inode, page,
  1393. p_s_path, p_s_item_key,
  1394. n_new_file_size, &c_mode);
  1395. if (c_mode == M_SKIP_BALANCING)
  1396. /* tail has been left in the unformatted node */
  1397. return n_ret_value;
  1398. n_is_inode_locked = 1;
  1399. /* removing of last unformatted node will change value we
  1400. have to return to truncate. Save it */
  1401. retval2 = n_ret_value;
  1402. /*retval2 = p_s_sb->s_blocksize - (n_new_file_size & (p_s_sb->s_blocksize - 1)); */
  1403. /* So, we have performed the first part of the conversion:
  1404. inserting the new direct item. Now we are removing the
  1405. last unformatted node pointer. Set key to search for
  1406. it. */
  1407. set_cpu_key_k_type(p_s_item_key, TYPE_INDIRECT);
  1408. p_s_item_key->key_length = 4;
  1409. n_new_file_size -=
  1410. (n_new_file_size & (p_s_sb->s_blocksize - 1));
  1411. tail_pos = n_new_file_size;
  1412. set_cpu_key_k_offset(p_s_item_key, n_new_file_size + 1);
  1413. if (search_for_position_by_key
  1414. (p_s_sb, p_s_item_key,
  1415. p_s_path) == POSITION_NOT_FOUND) {
  1416. print_block(PATH_PLAST_BUFFER(p_s_path), 3,
  1417. PATH_LAST_POSITION(p_s_path) - 1,
  1418. PATH_LAST_POSITION(p_s_path) + 1);
  1419. reiserfs_panic(p_s_sb,
  1420. "PAP-5580: reiserfs_cut_from_item: item to convert does not exist (%K)",
  1421. p_s_item_key);
  1422. }
  1423. continue;
  1424. }
  1425. if (n_cut_size == 0) {
  1426. pathrelse(p_s_path);
  1427. return 0;
  1428. }
  1429. s_cut_balance.insert_size[0] = n_cut_size;
  1430. n_ret_value = fix_nodes(c_mode, &s_cut_balance, NULL, NULL);
  1431. if (n_ret_value != REPEAT_SEARCH)
  1432. break;
  1433. PROC_INFO_INC(p_s_sb, cut_from_item_restarted);
  1434. n_ret_value =
  1435. search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path);
  1436. if (n_ret_value == POSITION_FOUND)
  1437. continue;
  1438. reiserfs_warning(p_s_sb,
  1439. "PAP-5610: reiserfs_cut_from_item: item %K not found",
  1440. p_s_item_key);
  1441. unfix_nodes(&s_cut_balance);
  1442. return (n_ret_value == IO_ERROR) ? -EIO : -ENOENT;
  1443. } /* while */
  1444. // check fix_nodes results (IO_ERROR or NO_DISK_SPACE)
  1445. if (n_ret_value != CARRY_ON) {
  1446. if (n_is_inode_locked) {
  1447. // FIXME: this seems to be not needed: we are always able
  1448. // to cut item
  1449. indirect_to_direct_roll_back(th, p_s_inode, p_s_path);
  1450. }
  1451. if (n_ret_value == NO_DISK_SPACE)
  1452. reiserfs_warning(p_s_sb, "NO_DISK_SPACE");
  1453. unfix_nodes(&s_cut_balance);
  1454. return -EIO;
  1455. }
  1456. /* go ahead and perform balancing */
  1457. RFALSE(c_mode == M_PASTE || c_mode == M_INSERT, "invalid mode");
  1458. /* Calculate number of bytes that need to be cut from the item. */
  1459. quota_cut_bytes =
  1460. (c_mode ==
  1461. M_DELETE) ? ih_item_len(get_ih(p_s_path)) : -s_cut_balance.
  1462. insert_size[0];
  1463. if (retval2 == -1)
  1464. n_ret_value = calc_deleted_bytes_number(&s_cut_balance, c_mode);
  1465. else
  1466. n_ret_value = retval2;
  1467. /* For direct items, we only change the quota when deleting the last
  1468. ** item.
  1469. */
  1470. p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path);
  1471. if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(p_le_ih)) {
  1472. if (c_mode == M_DELETE &&
  1473. (le_ih_k_offset(p_le_ih) & (p_s_sb->s_blocksize - 1)) ==
  1474. 1) {
  1475. // FIXME: this is to keep 3.5 happy
  1476. REISERFS_I(p_s_inode)->i_first_direct_byte = U32_MAX;
  1477. quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE;
  1478. } else {
  1479. quota_cut_bytes = 0;
  1480. }
  1481. }
  1482. #ifdef CONFIG_REISERFS_CHECK
  1483. if (n_is_inode_locked) {
  1484. struct item_head *le_ih =
  1485. PATH_PITEM_HEAD(s_cut_balance.tb_path);
  1486. /* we are going to complete indirect2direct conversion. Make
  1487. sure, that we exactly remove last unformatted node pointer
  1488. of the item */
  1489. if (!is_indirect_le_ih(le_ih))
  1490. reiserfs_panic(p_s_sb,
  1491. "vs-5652: reiserfs_cut_from_item: "
  1492. "item must be indirect %h", le_ih);
  1493. if (c_mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
  1494. reiserfs_panic(p_s_sb,
  1495. "vs-5653: reiserfs_cut_from_item: "
  1496. "completing indirect2direct conversion indirect item %h "
  1497. "being deleted must be of 4 byte long",
  1498. le_ih);
  1499. if (c_mode == M_CUT
  1500. && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
  1501. reiserfs_panic(p_s_sb,
  1502. "vs-5654: reiserfs_cut_from_item: "
  1503. "can not complete indirect2direct conversion of %h (CUT, insert_size==%d)",
  1504. le_ih, s_cut_balance.insert_size[0]);
  1505. }
  1506. /* it would be useful to make sure, that right neighboring
  1507. item is direct item of this file */
  1508. }
  1509. #endif
  1510. do_balance(&s_cut_balance, NULL, NULL, c_mode);
  1511. if (n_is_inode_locked) {
  1512. /* we've done an indirect->direct conversion. when the data block
  1513. ** was freed, it was removed from the list of blocks that must
  1514. ** be flushed before the transaction commits, make sure to
  1515. ** unmap and invalidate it
  1516. */
  1517. unmap_buffers(page, tail_pos);
  1518. REISERFS_I(p_s_inode)->i_flags &= ~i_pack_on_close_mask;
  1519. }
  1520. #ifdef REISERQUOTA_DEBUG
  1521. reiserfs_debug(p_s_inode->i_sb, REISERFS_DEBUG_CODE,
  1522. "reiserquota cut_from_item(): freeing %u id=%u type=%c",
  1523. quota_cut_bytes, p_s_inode->i_uid, '?');
  1524. #endif
  1525. DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes);
  1526. return n_ret_value;
  1527. }
  1528. static void truncate_directory(struct reiserfs_transaction_handle *th,
  1529. struct inode *inode)
  1530. {
  1531. BUG_ON(!th->t_trans_id);
  1532. if (inode->i_nlink)
  1533. reiserfs_warning(inode->i_sb,
  1534. "vs-5655: truncate_directory: link count != 0");
  1535. set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
  1536. set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
  1537. reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
  1538. reiserfs_update_sd(th, inode);
  1539. set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
  1540. set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
  1541. }
  1542. /* Truncate file to the new size. Note, this must be called with a transaction
  1543. already started */
  1544. int reiserfs_do_truncate(struct reiserfs_transaction_handle *th, struct inode *p_s_inode, /* ->i_size contains new
  1545. size */
  1546. struct page *page, /* up to date for last block */
  1547. int update_timestamps /* when it is called by
  1548. file_release to convert
  1549. the tail - no timestamps
  1550. should be updated */
  1551. )
  1552. {
  1553. INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
  1554. struct item_head *p_le_ih; /* Pointer to an item header. */
  1555. struct cpu_key s_item_key; /* Key to search for a previous file item. */
  1556. loff_t n_file_size, /* Old file size. */
  1557. n_new_file_size; /* New file size. */
  1558. int n_deleted; /* Number of deleted or truncated bytes. */
  1559. int retval;
  1560. int err = 0;
  1561. BUG_ON(!th->t_trans_id);
  1562. if (!
  1563. (S_ISREG(p_s_inode->i_mode) || S_ISDIR(p_s_inode->i_mode)
  1564. || S_ISLNK(p_s_inode->i_mode)))
  1565. return 0;
  1566. if (S_ISDIR(p_s_inode->i_mode)) {
  1567. // deletion of directory - no need to update timestamps
  1568. truncate_directory(th, p_s_inode);
  1569. return 0;
  1570. }
  1571. /* Get new file size. */
  1572. n_new_file_size = p_s_inode->i_size;
  1573. // FIXME: note, that key type is unimportant here
  1574. make_cpu_key(&s_item_key, p_s_inode, max_reiserfs_offset(p_s_inode),
  1575. TYPE_DIRECT, 3);
  1576. retval =
  1577. search_for_position_by_key(p_s_inode->i_sb, &s_item_key,
  1578. &s_search_path);
  1579. if (retval == IO_ERROR) {
  1580. reiserfs_warning(p_s_inode->i_sb,
  1581. "vs-5657: reiserfs_do_truncate: "
  1582. "i/o failure occurred trying to truncate %K",
  1583. &s_item_key);
  1584. err = -EIO;
  1585. goto out;
  1586. }
  1587. if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
  1588. reiserfs_warning(p_s_inode->i_sb,
  1589. "PAP-5660: reiserfs_do_truncate: "
  1590. "wrong result %d of search for %K", retval,
  1591. &s_item_key);
  1592. err = -EIO;
  1593. goto out;
  1594. }
  1595. s_search_path.pos_in_item--;
  1596. /* Get real file size (total length of all file items) */
  1597. p_le_ih = PATH_PITEM_HEAD(&s_search_path);
  1598. if (is_statdata_le_ih(p_le_ih))
  1599. n_file_size = 0;
  1600. else {
  1601. loff_t offset = le_ih_k_offset(p_le_ih);
  1602. int bytes =
  1603. op_bytes_number(p_le_ih, p_s_inode->i_sb->s_blocksize);
  1604. /* this may mismatch with real file size: if last direct item
  1605. had no padding zeros and last unformatted node had no free
  1606. space, this file would have this file size */
  1607. n_file_size = offset + bytes - 1;
  1608. }
  1609. /*
  1610. * are we doing a full truncate or delete, if so
  1611. * kick in the reada code
  1612. */
  1613. if (n_new_file_size == 0)
  1614. s_search_path.reada = PATH_READA | PATH_READA_BACK;
  1615. if (n_file_size == 0 || n_file_size < n_new_file_size) {
  1616. goto update_and_out;
  1617. }
  1618. /* Update key to search for the last file item. */
  1619. set_cpu_key_k_offset(&s_item_key, n_file_size);
  1620. do {
  1621. /* Cut or delete file item. */
  1622. n_deleted =
  1623. reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
  1624. p_s_inode, page, n_new_file_size);
  1625. if (n_deleted < 0) {
  1626. reiserfs_warning(p_s_inode->i_sb,
  1627. "vs-5665: reiserfs_do_truncate: reiserfs_cut_from_item failed");
  1628. reiserfs_check_path(&s_search_path);
  1629. return 0;
  1630. }
  1631. RFALSE(n_deleted > n_file_size,
  1632. "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
  1633. n_deleted, n_file_size, &s_item_key);
  1634. /* Change key to search the last file item. */
  1635. n_file_size -= n_deleted;
  1636. set_cpu_key_k_offset(&s_item_key, n_file_size);
  1637. /* While there are bytes to truncate and previous file item is presented in the tree. */
  1638. /*
  1639. ** This loop could take a really long time, and could log
  1640. ** many more blocks than a transaction can hold. So, we do a polite
  1641. ** journal end here, and if the transaction needs ending, we make
  1642. ** sure the file is consistent before ending the current trans
  1643. ** and starting a new one
  1644. */
  1645. if (journal_transaction_should_end(th, 0) ||
  1646. reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
  1647. int orig_len_alloc = th->t_blocks_allocated;
  1648. decrement_counters_in_path(&s_search_path);
  1649. if (update_timestamps) {
  1650. p_s_inode->i_mtime = p_s_inode->i_ctime =
  1651. CURRENT_TIME_SEC;
  1652. }
  1653. reiserfs_update_sd(th, p_s_inode);
  1654. err = journal_end(th, p_s_inode->i_sb, orig_len_alloc);
  1655. if (err)
  1656. goto out;
  1657. err = journal_begin(th, p_s_inode->i_sb,
  1658. JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
  1659. if (err)
  1660. goto out;
  1661. reiserfs_update_inode_transaction(p_s_inode);
  1662. }
  1663. } while (n_file_size > ROUND_UP(n_new_file_size) &&
  1664. search_for_position_by_key(p_s_inode->i_sb, &s_item_key,
  1665. &s_search_path) == POSITION_FOUND);
  1666. RFALSE(n_file_size > ROUND_UP(n_new_file_size),
  1667. "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
  1668. n_new_file_size, n_file_size, s_item_key.on_disk_key.k_objectid);
  1669. update_and_out:
  1670. if (update_timestamps) {
  1671. // this is truncate, not file closing
  1672. p_s_inode->i_mtime = p_s_inode->i_ctime = CURRENT_TIME_SEC;
  1673. }
  1674. reiserfs_update_sd(th, p_s_inode);
  1675. out:
  1676. pathrelse(&s_search_path);
  1677. return err;
  1678. }
  1679. #ifdef CONFIG_REISERFS_CHECK
  1680. // this makes sure, that we __append__, not overwrite or add holes
  1681. static void check_research_for_paste(struct treepath *path,
  1682. const struct cpu_key *p_s_key)
  1683. {
  1684. struct item_head *found_ih = get_ih(path);
  1685. if (is_direct_le_ih(found_ih)) {
  1686. if (le_ih_k_offset(found_ih) +
  1687. op_bytes_number(found_ih,
  1688. get_last_bh(path)->b_size) !=
  1689. cpu_key_k_offset(p_s_key)
  1690. || op_bytes_number(found_ih,
  1691. get_last_bh(path)->b_size) !=
  1692. pos_in_item(path))
  1693. reiserfs_panic(NULL,
  1694. "PAP-5720: check_research_for_paste: "
  1695. "found direct item %h or position (%d) does not match to key %K",
  1696. found_ih, pos_in_item(path), p_s_key);
  1697. }
  1698. if (is_indirect_le_ih(found_ih)) {
  1699. if (le_ih_k_offset(found_ih) +
  1700. op_bytes_number(found_ih,
  1701. get_last_bh(path)->b_size) !=
  1702. cpu_key_k_offset(p_s_key)
  1703. || I_UNFM_NUM(found_ih) != pos_in_item(path)
  1704. || get_ih_free_space(found_ih) != 0)
  1705. reiserfs_panic(NULL,
  1706. "PAP-5730: check_research_for_paste: "
  1707. "found indirect item (%h) or position (%d) does not match to key (%K)",
  1708. found_ih, pos_in_item(path), p_s_key);
  1709. }
  1710. }
  1711. #endif /* config reiserfs check */
  1712. /* Paste bytes to the existing item. Returns bytes number pasted into the item. */
  1713. int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_search_path, /* Path to the pasted item. */
  1714. const struct cpu_key *p_s_key, /* Key to search for the needed item. */
  1715. struct inode *inode, /* Inode item belongs to */
  1716. const char *p_c_body, /* Pointer to the bytes to paste. */
  1717. int n_pasted_size)
  1718. { /* Size of pasted bytes. */
  1719. struct tree_balance s_paste_balance;
  1720. int retval;
  1721. int fs_gen;
  1722. BUG_ON(!th->t_trans_id);
  1723. fs_gen = get_generation(inode->i_sb);
  1724. #ifdef REISERQUOTA_DEBUG
  1725. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1726. "reiserquota paste_into_item(): allocating %u id=%u type=%c",
  1727. n_pasted_size, inode->i_uid,
  1728. key2type(&(p_s_key->on_disk_key)));
  1729. #endif
  1730. if (DQUOT_ALLOC_SPACE_NODIRTY(inode, n_pasted_size)) {
  1731. pathrelse(p_s_search_path);
  1732. return -EDQUOT;
  1733. }
  1734. init_tb_struct(th, &s_paste_balance, th->t_super, p_s_search_path,
  1735. n_pasted_size);
  1736. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1737. s_paste_balance.key = p_s_key->on_disk_key;
  1738. #endif
  1739. /* DQUOT_* can schedule, must check before the fix_nodes */
  1740. if (fs_changed(fs_gen, inode->i_sb)) {
  1741. goto search_again;
  1742. }
  1743. while ((retval =
  1744. fix_nodes(M_PASTE, &s_paste_balance, NULL,
  1745. p_c_body)) == REPEAT_SEARCH) {
  1746. search_again:
  1747. /* file system changed while we were in the fix_nodes */
  1748. PROC_INFO_INC(th->t_super, paste_into_item_restarted);
  1749. retval =
  1750. search_for_position_by_key(th->t_super, p_s_key,
  1751. p_s_search_path);
  1752. if (retval == IO_ERROR) {
  1753. retval = -EIO;
  1754. goto error_out;
  1755. }
  1756. if (retval == POSITION_FOUND) {
  1757. reiserfs_warning(inode->i_sb,
  1758. "PAP-5710: reiserfs_paste_into_item: entry or pasted byte (%K) exists",
  1759. p_s_key);
  1760. retval = -EEXIST;
  1761. goto error_out;
  1762. }
  1763. #ifdef CONFIG_REISERFS_CHECK
  1764. check_research_for_paste(p_s_search_path, p_s_key);
  1765. #endif
  1766. }
  1767. /* Perform balancing after all resources are collected by fix_nodes, and
  1768. accessing them will not risk triggering schedule. */
  1769. if (retval == CARRY_ON) {
  1770. do_balance(&s_paste_balance, NULL /*ih */ , p_c_body, M_PASTE);
  1771. return 0;
  1772. }
  1773. retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
  1774. error_out:
  1775. /* this also releases the path */
  1776. unfix_nodes(&s_paste_balance);
  1777. #ifdef REISERQUOTA_DEBUG
  1778. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1779. "reiserquota paste_into_item(): freeing %u id=%u type=%c",
  1780. n_pasted_size, inode->i_uid,
  1781. key2type(&(p_s_key->on_disk_key)));
  1782. #endif
  1783. DQUOT_FREE_SPACE_NODIRTY(inode, n_pasted_size);
  1784. return retval;
  1785. }
  1786. /* Insert new item into the buffer at the path. */
  1787. int reiserfs_insert_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_path, /* Path to the inserteded item. */
  1788. const struct cpu_key *key, struct item_head *p_s_ih, /* Pointer to the item header to insert. */
  1789. struct inode *inode, const char *p_c_body)
  1790. { /* Pointer to the bytes to insert. */
  1791. struct tree_balance s_ins_balance;
  1792. int retval;
  1793. int fs_gen = 0;
  1794. int quota_bytes = 0;
  1795. BUG_ON(!th->t_trans_id);
  1796. if (inode) { /* Do we count quotas for item? */
  1797. fs_gen = get_generation(inode->i_sb);
  1798. quota_bytes = ih_item_len(p_s_ih);
  1799. /* hack so the quota code doesn't have to guess if the file has
  1800. ** a tail, links are always tails, so there's no guessing needed
  1801. */
  1802. if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_s_ih)) {
  1803. quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
  1804. }
  1805. #ifdef REISERQUOTA_DEBUG
  1806. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1807. "reiserquota insert_item(): allocating %u id=%u type=%c",
  1808. quota_bytes, inode->i_uid, head2type(p_s_ih));
  1809. #endif
  1810. /* We can't dirty inode here. It would be immediately written but
  1811. * appropriate stat item isn't inserted yet... */
  1812. if (DQUOT_ALLOC_SPACE_NODIRTY(inode, quota_bytes)) {
  1813. pathrelse(p_s_path);
  1814. return -EDQUOT;
  1815. }
  1816. }
  1817. init_tb_struct(th, &s_ins_balance, th->t_super, p_s_path,
  1818. IH_SIZE + ih_item_len(p_s_ih));
  1819. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1820. s_ins_balance.key = key->on_disk_key;
  1821. #endif
  1822. /* DQUOT_* can schedule, must check to be sure calling fix_nodes is safe */
  1823. if (inode && fs_changed(fs_gen, inode->i_sb)) {
  1824. goto search_again;
  1825. }
  1826. while ((retval =
  1827. fix_nodes(M_INSERT, &s_ins_balance, p_s_ih,
  1828. p_c_body)) == REPEAT_SEARCH) {
  1829. search_again:
  1830. /* file system changed while we were in the fix_nodes */
  1831. PROC_INFO_INC(th->t_super, insert_item_restarted);
  1832. retval = search_item(th->t_super, key, p_s_path);
  1833. if (retval == IO_ERROR) {
  1834. retval = -EIO;
  1835. goto error_out;
  1836. }
  1837. if (retval == ITEM_FOUND) {
  1838. reiserfs_warning(th->t_super,
  1839. "PAP-5760: reiserfs_insert_item: "
  1840. "key %K already exists in the tree",
  1841. key);
  1842. retval = -EEXIST;
  1843. goto error_out;
  1844. }
  1845. }
  1846. /* make balancing after all resources will be collected at a time */
  1847. if (retval == CARRY_ON) {
  1848. do_balance(&s_ins_balance, p_s_ih, p_c_body, M_INSERT);
  1849. return 0;
  1850. }
  1851. retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
  1852. error_out:
  1853. /* also releases the path */
  1854. unfix_nodes(&s_ins_balance);
  1855. #ifdef REISERQUOTA_DEBUG
  1856. reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
  1857. "reiserquota insert_item(): freeing %u id=%u type=%c",
  1858. quota_bytes, inode->i_uid, head2type(p_s_ih));
  1859. #endif
  1860. if (inode)
  1861. DQUOT_FREE_SPACE_NODIRTY(inode, quota_bytes);
  1862. return retval;
  1863. }