stree.c 65 KB

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