stree.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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 true if we have unlocked
  469. */
  470. static bool 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. bool unlocked = false;
  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 (!unlocked) {
  493. reiserfs_write_unlock(s);
  494. unlocked = true;
  495. }
  496. ll_rw_block(READA, 1, bh + j);
  497. }
  498. brelse(bh[j]);
  499. }
  500. return unlocked;
  501. }
  502. /**************************************************************************
  503. * Algorithm SearchByKey *
  504. * look for item in the Disk S+Tree by its key *
  505. * Input: sb - super block *
  506. * key - pointer to the key to search *
  507. * Output: ITEM_FOUND, ITEM_NOT_FOUND or IO_ERROR *
  508. * search_path - path from the root to the needed leaf *
  509. **************************************************************************/
  510. /* This function fills up the path from the root to the leaf as it
  511. descends the tree looking for the key. It uses reiserfs_bread to
  512. try to find buffers in the cache given their block number. If it
  513. does not find them in the cache it reads them from disk. For each
  514. node search_by_key finds using reiserfs_bread it then uses
  515. bin_search to look through that node. bin_search will find the
  516. position of the block_number of the next node if it is looking
  517. through an internal node. If it is looking through a leaf node
  518. bin_search will find the position of the item which has key either
  519. equal to given key, or which is the maximal key less than the given
  520. key. search_by_key returns a path that must be checked for the
  521. correctness of the top of the path but need not be checked for the
  522. correctness of the bottom of the path */
  523. /* The function is NOT SCHEDULE-SAFE! */
  524. int search_by_key(struct super_block *sb, const struct cpu_key *key, /* Key to search. */
  525. struct treepath *search_path,/* This structure was
  526. allocated and initialized
  527. by the calling
  528. function. It is filled up
  529. by this function. */
  530. int stop_level /* How far down the tree to search. To
  531. stop at leaf level - set to
  532. DISK_LEAF_NODE_LEVEL */
  533. )
  534. {
  535. b_blocknr_t block_number;
  536. int expected_level;
  537. struct buffer_head *bh;
  538. struct path_element *last_element;
  539. int node_level, retval;
  540. int right_neighbor_of_leaf_node;
  541. int fs_gen;
  542. struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
  543. b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
  544. int reada_count = 0;
  545. #ifdef CONFIG_REISERFS_CHECK
  546. int repeat_counter = 0;
  547. #endif
  548. PROC_INFO_INC(sb, search_by_key);
  549. /* As we add each node to a path we increase its count. This means that
  550. we must be careful to release all nodes in a path before we either
  551. discard the path struct or re-use the path struct, as we do here. */
  552. pathrelse(search_path);
  553. right_neighbor_of_leaf_node = 0;
  554. /* With each iteration of this loop we search through the items in the
  555. current node, and calculate the next current node(next path element)
  556. for the next iteration of this loop.. */
  557. block_number = SB_ROOT_BLOCK(sb);
  558. expected_level = -1;
  559. while (1) {
  560. #ifdef CONFIG_REISERFS_CHECK
  561. if (!(++repeat_counter % 50000))
  562. reiserfs_warning(sb, "PAP-5100",
  563. "%s: there were %d iterations of "
  564. "while loop looking for key %K",
  565. current->comm, repeat_counter,
  566. key);
  567. #endif
  568. /* prep path to have another element added to it. */
  569. last_element =
  570. PATH_OFFSET_PELEMENT(search_path,
  571. ++search_path->path_length);
  572. fs_gen = get_generation(sb);
  573. /* Read the next tree node, and set the last element in the path to
  574. have a pointer to it. */
  575. if ((bh = last_element->pe_buffer =
  576. sb_getblk(sb, block_number))) {
  577. bool unlocked = false;
  578. if (!buffer_uptodate(bh) && reada_count > 1)
  579. /* may unlock the write lock */
  580. unlocked = search_by_key_reada(sb, reada_bh,
  581. reada_blocks, reada_count);
  582. /*
  583. * If we haven't already unlocked the write lock,
  584. * then we need to do that here before reading
  585. * the current block
  586. */
  587. if (!buffer_uptodate(bh) && !unlocked) {
  588. reiserfs_write_unlock(sb);
  589. unlocked = true;
  590. }
  591. ll_rw_block(READ, 1, &bh);
  592. wait_on_buffer(bh);
  593. if (unlocked)
  594. reiserfs_write_lock(sb);
  595. if (!buffer_uptodate(bh))
  596. goto io_error;
  597. } else {
  598. io_error:
  599. search_path->path_length--;
  600. pathrelse(search_path);
  601. return IO_ERROR;
  602. }
  603. reada_count = 0;
  604. if (expected_level == -1)
  605. expected_level = SB_TREE_HEIGHT(sb);
  606. expected_level--;
  607. /* It is possible that schedule occurred. We must check whether the key
  608. to search is still in the tree rooted from the current buffer. If
  609. not then repeat search from the root. */
  610. if (fs_changed(fs_gen, sb) &&
  611. (!B_IS_IN_TREE(bh) ||
  612. B_LEVEL(bh) != expected_level ||
  613. !key_in_buffer(search_path, key, sb))) {
  614. PROC_INFO_INC(sb, search_by_key_fs_changed);
  615. PROC_INFO_INC(sb, search_by_key_restarted);
  616. PROC_INFO_INC(sb,
  617. sbk_restarted[expected_level - 1]);
  618. pathrelse(search_path);
  619. /* Get the root block number so that we can repeat the search
  620. starting from the root. */
  621. block_number = SB_ROOT_BLOCK(sb);
  622. expected_level = -1;
  623. right_neighbor_of_leaf_node = 0;
  624. /* repeat search from the root */
  625. continue;
  626. }
  627. /* only check that the key is in the buffer if key is not
  628. equal to the MAX_KEY. Latter case is only possible in
  629. "finish_unfinished()" processing during mount. */
  630. RFALSE(comp_keys(&MAX_KEY, key) &&
  631. !key_in_buffer(search_path, key, sb),
  632. "PAP-5130: key is not in the buffer");
  633. #ifdef CONFIG_REISERFS_CHECK
  634. if (REISERFS_SB(sb)->cur_tb) {
  635. print_cur_tb("5140");
  636. reiserfs_panic(sb, "PAP-5140",
  637. "schedule occurred in do_balance!");
  638. }
  639. #endif
  640. // make sure, that the node contents look like a node of
  641. // certain level
  642. if (!is_tree_node(bh, expected_level)) {
  643. reiserfs_error(sb, "vs-5150",
  644. "invalid format found in block %ld. "
  645. "Fsck?", bh->b_blocknr);
  646. pathrelse(search_path);
  647. return IO_ERROR;
  648. }
  649. /* ok, we have acquired next formatted node in the tree */
  650. node_level = B_LEVEL(bh);
  651. PROC_INFO_BH_STAT(sb, bh, node_level - 1);
  652. RFALSE(node_level < stop_level,
  653. "vs-5152: tree level (%d) is less than stop level (%d)",
  654. node_level, stop_level);
  655. retval = bin_search(key, B_N_PITEM_HEAD(bh, 0),
  656. B_NR_ITEMS(bh),
  657. (node_level ==
  658. DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
  659. KEY_SIZE,
  660. &(last_element->pe_position));
  661. if (node_level == stop_level) {
  662. return retval;
  663. }
  664. /* we are not in the stop level */
  665. if (retval == ITEM_FOUND)
  666. /* item has been found, so we choose the pointer which is to the right of the found one */
  667. last_element->pe_position++;
  668. /* if item was not found we choose the position which is to
  669. the left of the found item. This requires no code,
  670. bin_search did it already. */
  671. /* So we have chosen a position in the current node which is
  672. an internal node. Now we calculate child block number by
  673. position in the node. */
  674. block_number =
  675. B_N_CHILD_NUM(bh, last_element->pe_position);
  676. /* if we are going to read leaf nodes, try for read ahead as well */
  677. if ((search_path->reada & PATH_READA) &&
  678. node_level == DISK_LEAF_NODE_LEVEL + 1) {
  679. int pos = last_element->pe_position;
  680. int limit = B_NR_ITEMS(bh);
  681. struct reiserfs_key *le_key;
  682. if (search_path->reada & PATH_READA_BACK)
  683. limit = 0;
  684. while (reada_count < SEARCH_BY_KEY_READA) {
  685. if (pos == limit)
  686. break;
  687. reada_blocks[reada_count++] =
  688. B_N_CHILD_NUM(bh, pos);
  689. if (search_path->reada & PATH_READA_BACK)
  690. pos--;
  691. else
  692. pos++;
  693. /*
  694. * check to make sure we're in the same object
  695. */
  696. le_key = B_N_PDELIM_KEY(bh, pos);
  697. if (le32_to_cpu(le_key->k_objectid) !=
  698. key->on_disk_key.k_objectid) {
  699. break;
  700. }
  701. }
  702. }
  703. }
  704. }
  705. /* Form the path to an item and position in this item which contains
  706. file byte defined by key. If there is no such item
  707. corresponding to the key, we point the path to the item with
  708. maximal key less than key, and *pos_in_item is set to one
  709. past the last entry/byte in the item. If searching for entry in a
  710. directory item, and it is not found, *pos_in_item is set to one
  711. entry more than the entry with maximal key which is less than the
  712. sought key.
  713. Note that if there is no entry in this same node which is one more,
  714. then we point to an imaginary entry. for direct items, the
  715. position is in units of bytes, for indirect items the position is
  716. in units of blocknr entries, for directory items the position is in
  717. units of directory entries. */
  718. /* The function is NOT SCHEDULE-SAFE! */
  719. int search_for_position_by_key(struct super_block *sb, /* Pointer to the super block. */
  720. const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */
  721. struct treepath *search_path /* Filled up by this function. */
  722. )
  723. {
  724. struct item_head *p_le_ih; /* pointer to on-disk structure */
  725. int blk_size;
  726. loff_t item_offset, offset;
  727. struct reiserfs_dir_entry de;
  728. int retval;
  729. /* If searching for directory entry. */
  730. if (is_direntry_cpu_key(p_cpu_key))
  731. return search_by_entry_key(sb, p_cpu_key, search_path,
  732. &de);
  733. /* If not searching for directory entry. */
  734. /* If item is found. */
  735. retval = search_item(sb, p_cpu_key, search_path);
  736. if (retval == IO_ERROR)
  737. return retval;
  738. if (retval == ITEM_FOUND) {
  739. RFALSE(!ih_item_len
  740. (B_N_PITEM_HEAD
  741. (PATH_PLAST_BUFFER(search_path),
  742. PATH_LAST_POSITION(search_path))),
  743. "PAP-5165: item length equals zero");
  744. pos_in_item(search_path) = 0;
  745. return POSITION_FOUND;
  746. }
  747. RFALSE(!PATH_LAST_POSITION(search_path),
  748. "PAP-5170: position equals zero");
  749. /* Item is not found. Set path to the previous item. */
  750. p_le_ih =
  751. B_N_PITEM_HEAD(PATH_PLAST_BUFFER(search_path),
  752. --PATH_LAST_POSITION(search_path));
  753. blk_size = sb->s_blocksize;
  754. if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
  755. return FILE_NOT_FOUND;
  756. }
  757. // FIXME: quite ugly this far
  758. item_offset = le_ih_k_offset(p_le_ih);
  759. offset = cpu_key_k_offset(p_cpu_key);
  760. /* Needed byte is contained in the item pointed to by the path. */
  761. if (item_offset <= offset &&
  762. item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
  763. pos_in_item(search_path) = offset - item_offset;
  764. if (is_indirect_le_ih(p_le_ih)) {
  765. pos_in_item(search_path) /= blk_size;
  766. }
  767. return POSITION_FOUND;
  768. }
  769. /* Needed byte is not contained in the item pointed to by the
  770. path. Set pos_in_item out of the item. */
  771. if (is_indirect_le_ih(p_le_ih))
  772. pos_in_item(search_path) =
  773. ih_item_len(p_le_ih) / UNFM_P_SIZE;
  774. else
  775. pos_in_item(search_path) = ih_item_len(p_le_ih);
  776. return POSITION_NOT_FOUND;
  777. }
  778. /* Compare given item and item pointed to by the path. */
  779. int comp_items(const struct item_head *stored_ih, const struct treepath *path)
  780. {
  781. struct buffer_head *bh = PATH_PLAST_BUFFER(path);
  782. struct item_head *ih;
  783. /* Last buffer at the path is not in the tree. */
  784. if (!B_IS_IN_TREE(bh))
  785. return 1;
  786. /* Last path position is invalid. */
  787. if (PATH_LAST_POSITION(path) >= B_NR_ITEMS(bh))
  788. return 1;
  789. /* we need only to know, whether it is the same item */
  790. ih = get_ih(path);
  791. return memcmp(stored_ih, ih, IH_SIZE);
  792. }
  793. /* unformatted nodes are not logged anymore, ever. This is safe
  794. ** now
  795. */
  796. #define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
  797. // block can not be forgotten as it is in I/O or held by someone
  798. #define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
  799. // prepare for delete or cut of direct item
  800. static inline int prepare_for_direct_item(struct treepath *path,
  801. struct item_head *le_ih,
  802. struct inode *inode,
  803. loff_t new_file_length, int *cut_size)
  804. {
  805. loff_t round_len;
  806. if (new_file_length == max_reiserfs_offset(inode)) {
  807. /* item has to be deleted */
  808. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  809. return M_DELETE;
  810. }
  811. // new file gets truncated
  812. if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
  813. //
  814. round_len = ROUND_UP(new_file_length);
  815. /* this was new_file_length < le_ih ... */
  816. if (round_len < le_ih_k_offset(le_ih)) {
  817. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  818. return M_DELETE; /* Delete this item. */
  819. }
  820. /* Calculate first position and size for cutting from item. */
  821. pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
  822. *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
  823. return M_CUT; /* Cut from this item. */
  824. }
  825. // old file: items may have any length
  826. if (new_file_length < le_ih_k_offset(le_ih)) {
  827. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  828. return M_DELETE; /* Delete this item. */
  829. }
  830. /* Calculate first position and size for cutting from item. */
  831. *cut_size = -(ih_item_len(le_ih) -
  832. (pos_in_item(path) =
  833. new_file_length + 1 - le_ih_k_offset(le_ih)));
  834. return M_CUT; /* Cut from this item. */
  835. }
  836. static inline int prepare_for_direntry_item(struct treepath *path,
  837. struct item_head *le_ih,
  838. struct inode *inode,
  839. loff_t new_file_length,
  840. int *cut_size)
  841. {
  842. if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
  843. new_file_length == max_reiserfs_offset(inode)) {
  844. RFALSE(ih_entry_count(le_ih) != 2,
  845. "PAP-5220: incorrect empty directory item (%h)", le_ih);
  846. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  847. return M_DELETE; /* Delete the directory item containing "." and ".." entry. */
  848. }
  849. if (ih_entry_count(le_ih) == 1) {
  850. /* Delete the directory item such as there is one record only
  851. in this item */
  852. *cut_size = -(IH_SIZE + ih_item_len(le_ih));
  853. return M_DELETE;
  854. }
  855. /* Cut one record from the directory item. */
  856. *cut_size =
  857. -(DEH_SIZE +
  858. entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
  859. return M_CUT;
  860. }
  861. #define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
  862. /* If the path points to a directory or direct item, calculate mode and the size cut, for balance.
  863. If the path points to an indirect item, remove some number of its unformatted nodes.
  864. In case of file truncate calculate whether this item must be deleted/truncated or last
  865. unformatted node of this item will be converted to a direct item.
  866. This function returns a determination of what balance mode the calling function should employ. */
  867. 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
  868. from end of the file. */
  869. int *cut_size, unsigned long long new_file_length /* MAX_KEY_OFFSET in case of delete. */
  870. )
  871. {
  872. struct super_block *sb = inode->i_sb;
  873. struct item_head *p_le_ih = PATH_PITEM_HEAD(path);
  874. struct buffer_head *bh = PATH_PLAST_BUFFER(path);
  875. BUG_ON(!th->t_trans_id);
  876. /* Stat_data item. */
  877. if (is_statdata_le_ih(p_le_ih)) {
  878. RFALSE(new_file_length != max_reiserfs_offset(inode),
  879. "PAP-5210: mode must be M_DELETE");
  880. *cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
  881. return M_DELETE;
  882. }
  883. /* Directory item. */
  884. if (is_direntry_le_ih(p_le_ih))
  885. return prepare_for_direntry_item(path, p_le_ih, inode,
  886. new_file_length,
  887. cut_size);
  888. /* Direct item. */
  889. if (is_direct_le_ih(p_le_ih))
  890. return prepare_for_direct_item(path, p_le_ih, inode,
  891. new_file_length, cut_size);
  892. /* Case of an indirect item. */
  893. {
  894. int blk_size = sb->s_blocksize;
  895. struct item_head s_ih;
  896. int need_re_search;
  897. int delete = 0;
  898. int result = M_CUT;
  899. int pos = 0;
  900. if ( new_file_length == max_reiserfs_offset (inode) ) {
  901. /* prepare_for_delete_or_cut() is called by
  902. * reiserfs_delete_item() */
  903. new_file_length = 0;
  904. delete = 1;
  905. }
  906. do {
  907. need_re_search = 0;
  908. *cut_size = 0;
  909. bh = PATH_PLAST_BUFFER(path);
  910. copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
  911. pos = I_UNFM_NUM(&s_ih);
  912. while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
  913. __le32 *unfm;
  914. __u32 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 && (*cut_size) != 0 &&
  919. reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)
  920. break;
  921. unfm = (__le32 *)B_I_PITEM(bh, &s_ih) + pos - 1;
  922. block = get_block_num(unfm, 0);
  923. if (block != 0) {
  924. reiserfs_prepare_for_journal(sb, bh, 1);
  925. put_block_num(unfm, 0, 0);
  926. journal_mark_dirty(th, sb, bh);
  927. reiserfs_free_block(th, inode, block, 1);
  928. }
  929. reiserfs_write_unlock(sb);
  930. cond_resched();
  931. reiserfs_write_lock(sb);
  932. if (item_moved (&s_ih, path)) {
  933. need_re_search = 1;
  934. break;
  935. }
  936. pos --;
  937. (*removed)++;
  938. (*cut_size) -= UNFM_P_SIZE;
  939. if (pos == 0) {
  940. (*cut_size) -= IH_SIZE;
  941. result = M_DELETE;
  942. break;
  943. }
  944. }
  945. /* a trick. If the buffer has been logged, this will do nothing. If
  946. ** we've broken the loop without logging it, it will restore the
  947. ** buffer */
  948. reiserfs_restore_prepared_buffer(sb, bh);
  949. } while (need_re_search &&
  950. search_for_position_by_key(sb, item_key, path) == POSITION_FOUND);
  951. pos_in_item(path) = pos * UNFM_P_SIZE;
  952. if (*cut_size == 0) {
  953. /* Nothing were cut. maybe convert last unformatted node to the
  954. * direct item? */
  955. result = M_CONVERT;
  956. }
  957. return result;
  958. }
  959. }
  960. /* Calculate number of bytes which will be deleted or cut during balance */
  961. static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
  962. {
  963. int del_size;
  964. struct item_head *p_le_ih = PATH_PITEM_HEAD(tb->tb_path);
  965. if (is_statdata_le_ih(p_le_ih))
  966. return 0;
  967. del_size =
  968. (mode ==
  969. M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
  970. if (is_direntry_le_ih(p_le_ih)) {
  971. /* return EMPTY_DIR_SIZE; We delete emty directoris only.
  972. * we can't use EMPTY_DIR_SIZE, as old format dirs have a different
  973. * empty size. ick. FIXME, is this right? */
  974. return del_size;
  975. }
  976. if (is_indirect_le_ih(p_le_ih))
  977. del_size = (del_size / UNFM_P_SIZE) *
  978. (PATH_PLAST_BUFFER(tb->tb_path)->b_size);
  979. return del_size;
  980. }
  981. static void init_tb_struct(struct reiserfs_transaction_handle *th,
  982. struct tree_balance *tb,
  983. struct super_block *sb,
  984. struct treepath *path, int size)
  985. {
  986. BUG_ON(!th->t_trans_id);
  987. memset(tb, '\0', sizeof(struct tree_balance));
  988. tb->transaction_handle = th;
  989. tb->tb_sb = sb;
  990. tb->tb_path = path;
  991. PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
  992. PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
  993. tb->insert_size[0] = size;
  994. }
  995. void padd_item(char *item, int total_length, int length)
  996. {
  997. int i;
  998. for (i = total_length; i > length;)
  999. item[--i] = 0;
  1000. }
  1001. #ifdef REISERQUOTA_DEBUG
  1002. char key2type(struct reiserfs_key *ih)
  1003. {
  1004. if (is_direntry_le_key(2, ih))
  1005. return 'd';
  1006. if (is_direct_le_key(2, ih))
  1007. return 'D';
  1008. if (is_indirect_le_key(2, ih))
  1009. return 'i';
  1010. if (is_statdata_le_key(2, ih))
  1011. return 's';
  1012. return 'u';
  1013. }
  1014. char head2type(struct item_head *ih)
  1015. {
  1016. if (is_direntry_le_ih(ih))
  1017. return 'd';
  1018. if (is_direct_le_ih(ih))
  1019. return 'D';
  1020. if (is_indirect_le_ih(ih))
  1021. return 'i';
  1022. if (is_statdata_le_ih(ih))
  1023. return 's';
  1024. return 'u';
  1025. }
  1026. #endif
  1027. /* Delete object item.
  1028. * th - active transaction handle
  1029. * path - path to the deleted item
  1030. * item_key - key to search for the deleted item
  1031. * indode - used for updating i_blocks and quotas
  1032. * un_bh - NULL or unformatted node pointer
  1033. */
  1034. int reiserfs_delete_item(struct reiserfs_transaction_handle *th,
  1035. struct treepath *path, const struct cpu_key *item_key,
  1036. struct inode *inode, struct buffer_head *un_bh)
  1037. {
  1038. struct super_block *sb = inode->i_sb;
  1039. struct tree_balance s_del_balance;
  1040. struct item_head s_ih;
  1041. struct item_head *q_ih;
  1042. int quota_cut_bytes;
  1043. int ret_value, del_size, removed;
  1044. #ifdef CONFIG_REISERFS_CHECK
  1045. char mode;
  1046. int iter = 0;
  1047. #endif
  1048. BUG_ON(!th->t_trans_id);
  1049. init_tb_struct(th, &s_del_balance, sb, path,
  1050. 0 /*size is unknown */ );
  1051. while (1) {
  1052. removed = 0;
  1053. #ifdef CONFIG_REISERFS_CHECK
  1054. iter++;
  1055. mode =
  1056. #endif
  1057. prepare_for_delete_or_cut(th, inode, path,
  1058. item_key, &removed,
  1059. &del_size,
  1060. max_reiserfs_offset(inode));
  1061. RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
  1062. copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
  1063. s_del_balance.insert_size[0] = del_size;
  1064. ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
  1065. if (ret_value != REPEAT_SEARCH)
  1066. break;
  1067. PROC_INFO_INC(sb, delete_item_restarted);
  1068. // file system changed, repeat search
  1069. ret_value =
  1070. search_for_position_by_key(sb, item_key, path);
  1071. if (ret_value == IO_ERROR)
  1072. break;
  1073. if (ret_value == FILE_NOT_FOUND) {
  1074. reiserfs_warning(sb, "vs-5340",
  1075. "no items of the file %K found",
  1076. item_key);
  1077. break;
  1078. }
  1079. } /* while (1) */
  1080. if (ret_value != CARRY_ON) {
  1081. unfix_nodes(&s_del_balance);
  1082. return 0;
  1083. }
  1084. // reiserfs_delete_item returns item length when success
  1085. ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
  1086. q_ih = get_ih(path);
  1087. quota_cut_bytes = ih_item_len(q_ih);
  1088. /* hack so the quota code doesn't have to guess if the file
  1089. ** has a tail. On tail insert, we allocate quota for 1 unformatted node.
  1090. ** We test the offset because the tail might have been
  1091. ** split into multiple items, and we only want to decrement for
  1092. ** the unfm node once
  1093. */
  1094. if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(q_ih)) {
  1095. if ((le_ih_k_offset(q_ih) & (sb->s_blocksize - 1)) == 1) {
  1096. quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
  1097. } else {
  1098. quota_cut_bytes = 0;
  1099. }
  1100. }
  1101. if (un_bh) {
  1102. int off;
  1103. char *data;
  1104. /* We are in direct2indirect conversion, so move tail contents
  1105. to the unformatted node */
  1106. /* note, we do the copy before preparing the buffer because we
  1107. ** don't care about the contents of the unformatted node yet.
  1108. ** the only thing we really care about is the direct item's data
  1109. ** is in the unformatted node.
  1110. **
  1111. ** Otherwise, we would have to call reiserfs_prepare_for_journal on
  1112. ** the unformatted node, which might schedule, meaning we'd have to
  1113. ** loop all the way back up to the start of the while loop.
  1114. **
  1115. ** The unformatted node must be dirtied later on. We can't be
  1116. ** sure here if the entire tail has been deleted yet.
  1117. **
  1118. ** un_bh is from the page cache (all unformatted nodes are
  1119. ** from the page cache) and might be a highmem page. So, we
  1120. ** can't use un_bh->b_data.
  1121. ** -clm
  1122. */
  1123. data = kmap_atomic(un_bh->b_page);
  1124. off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1));
  1125. memcpy(data + off,
  1126. B_I_PITEM(PATH_PLAST_BUFFER(path), &s_ih),
  1127. ret_value);
  1128. kunmap_atomic(data);
  1129. }
  1130. /* Perform balancing after all resources have been collected at once. */
  1131. do_balance(&s_del_balance, NULL, NULL, M_DELETE);
  1132. #ifdef REISERQUOTA_DEBUG
  1133. reiserfs_debug(sb, REISERFS_DEBUG_CODE,
  1134. "reiserquota delete_item(): freeing %u, id=%u type=%c",
  1135. quota_cut_bytes, inode->i_uid, head2type(&s_ih));
  1136. #endif
  1137. dquot_free_space_nodirty(inode, quota_cut_bytes);
  1138. /* Return deleted body length */
  1139. return ret_value;
  1140. }
  1141. /* Summary Of Mechanisms For Handling Collisions Between Processes:
  1142. deletion of the body of the object is performed by iput(), with the
  1143. result that if multiple processes are operating on a file, the
  1144. deletion of the body of the file is deferred until the last process
  1145. that has an open inode performs its iput().
  1146. writes and truncates are protected from collisions by use of
  1147. semaphores.
  1148. creates, linking, and mknod are protected from collisions with other
  1149. processes by making the reiserfs_add_entry() the last step in the
  1150. creation, and then rolling back all changes if there was a collision.
  1151. - Hans
  1152. */
  1153. /* this deletes item which never gets split */
  1154. void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
  1155. struct inode *inode, struct reiserfs_key *key)
  1156. {
  1157. struct tree_balance tb;
  1158. INITIALIZE_PATH(path);
  1159. int item_len = 0;
  1160. int tb_init = 0;
  1161. struct cpu_key cpu_key;
  1162. int retval;
  1163. int quota_cut_bytes = 0;
  1164. BUG_ON(!th->t_trans_id);
  1165. le_key2cpu_key(&cpu_key, key);
  1166. while (1) {
  1167. retval = search_item(th->t_super, &cpu_key, &path);
  1168. if (retval == IO_ERROR) {
  1169. reiserfs_error(th->t_super, "vs-5350",
  1170. "i/o failure occurred trying "
  1171. "to delete %K", &cpu_key);
  1172. break;
  1173. }
  1174. if (retval != ITEM_FOUND) {
  1175. pathrelse(&path);
  1176. // No need for a warning, if there is just no free space to insert '..' item into the newly-created subdir
  1177. if (!
  1178. ((unsigned long long)
  1179. GET_HASH_VALUE(le_key_k_offset
  1180. (le_key_version(key), key)) == 0
  1181. && (unsigned long long)
  1182. GET_GENERATION_NUMBER(le_key_k_offset
  1183. (le_key_version(key),
  1184. key)) == 1))
  1185. reiserfs_warning(th->t_super, "vs-5355",
  1186. "%k not found", key);
  1187. break;
  1188. }
  1189. if (!tb_init) {
  1190. tb_init = 1;
  1191. item_len = ih_item_len(PATH_PITEM_HEAD(&path));
  1192. init_tb_struct(th, &tb, th->t_super, &path,
  1193. -(IH_SIZE + item_len));
  1194. }
  1195. quota_cut_bytes = ih_item_len(PATH_PITEM_HEAD(&path));
  1196. retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
  1197. if (retval == REPEAT_SEARCH) {
  1198. PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
  1199. continue;
  1200. }
  1201. if (retval == CARRY_ON) {
  1202. do_balance(&tb, NULL, NULL, M_DELETE);
  1203. if (inode) { /* Should we count quota for item? (we don't count quotas for save-links) */
  1204. #ifdef REISERQUOTA_DEBUG
  1205. reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
  1206. "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
  1207. quota_cut_bytes, inode->i_uid,
  1208. key2type(key));
  1209. #endif
  1210. dquot_free_space_nodirty(inode,
  1211. quota_cut_bytes);
  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. BUG_ON(!th->t_trans_id);
  1373. init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
  1374. 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. mode =
  1383. prepare_for_delete_or_cut(th, inode, path,
  1384. item_key, &removed,
  1385. &cut_size, new_file_size);
  1386. if (mode == M_CONVERT) {
  1387. /* convert last unformatted node to direct item or leave
  1388. tail in the unformatted node */
  1389. RFALSE(ret_value != CARRY_ON,
  1390. "PAP-5570: can not convert twice");
  1391. ret_value =
  1392. maybe_indirect_to_direct(th, inode, page,
  1393. path, item_key,
  1394. new_file_size, &mode);
  1395. if (mode == M_SKIP_BALANCING)
  1396. /* tail has been left in the unformatted node */
  1397. return ret_value;
  1398. is_inode_locked = 1;
  1399. /* removing of last unformatted node will change value we
  1400. have to return to truncate. Save it */
  1401. retval2 = ret_value;
  1402. /*retval2 = sb->s_blocksize - (new_file_size & (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(item_key, TYPE_INDIRECT);
  1408. item_key->key_length = 4;
  1409. new_file_size -=
  1410. (new_file_size & (sb->s_blocksize - 1));
  1411. tail_pos = new_file_size;
  1412. set_cpu_key_k_offset(item_key, new_file_size + 1);
  1413. if (search_for_position_by_key
  1414. (sb, item_key,
  1415. path) == POSITION_NOT_FOUND) {
  1416. print_block(PATH_PLAST_BUFFER(path), 3,
  1417. PATH_LAST_POSITION(path) - 1,
  1418. PATH_LAST_POSITION(path) + 1);
  1419. reiserfs_panic(sb, "PAP-5580", "item to "
  1420. "convert does not exist (%K)",
  1421. item_key);
  1422. }
  1423. continue;
  1424. }
  1425. if (cut_size == 0) {
  1426. pathrelse(path);
  1427. return 0;
  1428. }
  1429. s_cut_balance.insert_size[0] = cut_size;
  1430. ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
  1431. if (ret_value != REPEAT_SEARCH)
  1432. break;
  1433. PROC_INFO_INC(sb, cut_from_item_restarted);
  1434. ret_value =
  1435. search_for_position_by_key(sb, item_key, path);
  1436. if (ret_value == POSITION_FOUND)
  1437. continue;
  1438. reiserfs_warning(sb, "PAP-5610", "item %K not found",
  1439. item_key);
  1440. unfix_nodes(&s_cut_balance);
  1441. return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
  1442. } /* while */
  1443. // check fix_nodes results (IO_ERROR or NO_DISK_SPACE)
  1444. if (ret_value != CARRY_ON) {
  1445. if (is_inode_locked) {
  1446. // FIXME: this seems to be not needed: we are always able
  1447. // to cut item
  1448. indirect_to_direct_roll_back(th, inode, path);
  1449. }
  1450. if (ret_value == NO_DISK_SPACE)
  1451. reiserfs_warning(sb, "reiserfs-5092",
  1452. "NO_DISK_SPACE");
  1453. unfix_nodes(&s_cut_balance);
  1454. return -EIO;
  1455. }
  1456. /* go ahead and perform balancing */
  1457. RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
  1458. /* Calculate number of bytes that need to be cut from the item. */
  1459. quota_cut_bytes =
  1460. (mode ==
  1461. M_DELETE) ? ih_item_len(get_ih(path)) : -s_cut_balance.
  1462. insert_size[0];
  1463. if (retval2 == -1)
  1464. ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
  1465. else
  1466. 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(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
  1472. if (mode == M_DELETE &&
  1473. (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
  1474. 1) {
  1475. // FIXME: this is to keep 3.5 happy
  1476. REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
  1477. quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
  1478. } else {
  1479. quota_cut_bytes = 0;
  1480. }
  1481. }
  1482. #ifdef CONFIG_REISERFS_CHECK
  1483. if (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(sb, "vs-5652",
  1491. "item must be indirect %h", le_ih);
  1492. if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
  1493. reiserfs_panic(sb, "vs-5653", "completing "
  1494. "indirect2direct conversion indirect "
  1495. "item %h being deleted must be of "
  1496. "4 byte long", le_ih);
  1497. if (mode == M_CUT
  1498. && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
  1499. reiserfs_panic(sb, "vs-5654", "can not complete "
  1500. "indirect2direct conversion of %h "
  1501. "(CUT, insert_size==%d)",
  1502. le_ih, s_cut_balance.insert_size[0]);
  1503. }
  1504. /* it would be useful to make sure, that right neighboring
  1505. item is direct item of this file */
  1506. }
  1507. #endif
  1508. do_balance(&s_cut_balance, NULL, NULL, mode);
  1509. if (is_inode_locked) {
  1510. /* we've done an indirect->direct conversion. when the data block
  1511. ** was freed, it was removed from the list of blocks that must
  1512. ** be flushed before the transaction commits, make sure to
  1513. ** unmap and invalidate it
  1514. */
  1515. unmap_buffers(page, tail_pos);
  1516. REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
  1517. }
  1518. #ifdef REISERQUOTA_DEBUG
  1519. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1520. "reiserquota cut_from_item(): freeing %u id=%u type=%c",
  1521. quota_cut_bytes, inode->i_uid, '?');
  1522. #endif
  1523. dquot_free_space_nodirty(inode, quota_cut_bytes);
  1524. return ret_value;
  1525. }
  1526. static void truncate_directory(struct reiserfs_transaction_handle *th,
  1527. struct inode *inode)
  1528. {
  1529. BUG_ON(!th->t_trans_id);
  1530. if (inode->i_nlink)
  1531. reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");
  1532. set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
  1533. set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
  1534. reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
  1535. reiserfs_update_sd(th, inode);
  1536. set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
  1537. set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
  1538. }
  1539. /* Truncate file to the new size. Note, this must be called with a transaction
  1540. already started */
  1541. int reiserfs_do_truncate(struct reiserfs_transaction_handle *th,
  1542. struct inode *inode, /* ->i_size contains new size */
  1543. struct page *page, /* up to date for last block */
  1544. int update_timestamps /* when it is called by
  1545. file_release to convert
  1546. the tail - no timestamps
  1547. should be updated */
  1548. )
  1549. {
  1550. INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
  1551. struct item_head *p_le_ih; /* Pointer to an item header. */
  1552. struct cpu_key s_item_key; /* Key to search for a previous file item. */
  1553. loff_t file_size, /* Old file size. */
  1554. new_file_size; /* New file size. */
  1555. int deleted; /* Number of deleted or truncated bytes. */
  1556. int retval;
  1557. int err = 0;
  1558. BUG_ON(!th->t_trans_id);
  1559. if (!
  1560. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1561. || S_ISLNK(inode->i_mode)))
  1562. return 0;
  1563. if (S_ISDIR(inode->i_mode)) {
  1564. // deletion of directory - no need to update timestamps
  1565. truncate_directory(th, inode);
  1566. return 0;
  1567. }
  1568. /* Get new file size. */
  1569. new_file_size = inode->i_size;
  1570. // FIXME: note, that key type is unimportant here
  1571. make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
  1572. TYPE_DIRECT, 3);
  1573. retval =
  1574. search_for_position_by_key(inode->i_sb, &s_item_key,
  1575. &s_search_path);
  1576. if (retval == IO_ERROR) {
  1577. reiserfs_error(inode->i_sb, "vs-5657",
  1578. "i/o failure occurred trying to truncate %K",
  1579. &s_item_key);
  1580. err = -EIO;
  1581. goto out;
  1582. }
  1583. if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
  1584. reiserfs_error(inode->i_sb, "PAP-5660",
  1585. "wrong result %d of search for %K", retval,
  1586. &s_item_key);
  1587. err = -EIO;
  1588. goto out;
  1589. }
  1590. s_search_path.pos_in_item--;
  1591. /* Get real file size (total length of all file items) */
  1592. p_le_ih = PATH_PITEM_HEAD(&s_search_path);
  1593. if (is_statdata_le_ih(p_le_ih))
  1594. file_size = 0;
  1595. else {
  1596. loff_t offset = le_ih_k_offset(p_le_ih);
  1597. int bytes =
  1598. op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
  1599. /* this may mismatch with real file size: if last direct item
  1600. had no padding zeros and last unformatted node had no free
  1601. space, this file would have this file size */
  1602. file_size = offset + bytes - 1;
  1603. }
  1604. /*
  1605. * are we doing a full truncate or delete, if so
  1606. * kick in the reada code
  1607. */
  1608. if (new_file_size == 0)
  1609. s_search_path.reada = PATH_READA | PATH_READA_BACK;
  1610. if (file_size == 0 || file_size < new_file_size) {
  1611. goto update_and_out;
  1612. }
  1613. /* Update key to search for the last file item. */
  1614. set_cpu_key_k_offset(&s_item_key, file_size);
  1615. do {
  1616. /* Cut or delete file item. */
  1617. deleted =
  1618. reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
  1619. inode, page, new_file_size);
  1620. if (deleted < 0) {
  1621. reiserfs_warning(inode->i_sb, "vs-5665",
  1622. "reiserfs_cut_from_item failed");
  1623. reiserfs_check_path(&s_search_path);
  1624. return 0;
  1625. }
  1626. RFALSE(deleted > file_size,
  1627. "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
  1628. deleted, file_size, &s_item_key);
  1629. /* Change key to search the last file item. */
  1630. file_size -= deleted;
  1631. set_cpu_key_k_offset(&s_item_key, file_size);
  1632. /* While there are bytes to truncate and previous file item is presented in the tree. */
  1633. /*
  1634. ** This loop could take a really long time, and could log
  1635. ** many more blocks than a transaction can hold. So, we do a polite
  1636. ** journal end here, and if the transaction needs ending, we make
  1637. ** sure the file is consistent before ending the current trans
  1638. ** and starting a new one
  1639. */
  1640. if (journal_transaction_should_end(th, 0) ||
  1641. reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
  1642. int orig_len_alloc = th->t_blocks_allocated;
  1643. pathrelse(&s_search_path);
  1644. if (update_timestamps) {
  1645. inode->i_mtime = CURRENT_TIME_SEC;
  1646. inode->i_ctime = CURRENT_TIME_SEC;
  1647. }
  1648. reiserfs_update_sd(th, inode);
  1649. err = journal_end(th, inode->i_sb, orig_len_alloc);
  1650. if (err)
  1651. goto out;
  1652. err = journal_begin(th, inode->i_sb,
  1653. JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
  1654. if (err)
  1655. goto out;
  1656. reiserfs_update_inode_transaction(inode);
  1657. }
  1658. } while (file_size > ROUND_UP(new_file_size) &&
  1659. search_for_position_by_key(inode->i_sb, &s_item_key,
  1660. &s_search_path) == POSITION_FOUND);
  1661. RFALSE(file_size > ROUND_UP(new_file_size),
  1662. "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
  1663. new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
  1664. update_and_out:
  1665. if (update_timestamps) {
  1666. // this is truncate, not file closing
  1667. inode->i_mtime = CURRENT_TIME_SEC;
  1668. inode->i_ctime = CURRENT_TIME_SEC;
  1669. }
  1670. reiserfs_update_sd(th, inode);
  1671. out:
  1672. pathrelse(&s_search_path);
  1673. return err;
  1674. }
  1675. #ifdef CONFIG_REISERFS_CHECK
  1676. // this makes sure, that we __append__, not overwrite or add holes
  1677. static void check_research_for_paste(struct treepath *path,
  1678. const struct cpu_key *key)
  1679. {
  1680. struct item_head *found_ih = get_ih(path);
  1681. if (is_direct_le_ih(found_ih)) {
  1682. if (le_ih_k_offset(found_ih) +
  1683. op_bytes_number(found_ih,
  1684. get_last_bh(path)->b_size) !=
  1685. cpu_key_k_offset(key)
  1686. || op_bytes_number(found_ih,
  1687. get_last_bh(path)->b_size) !=
  1688. pos_in_item(path))
  1689. reiserfs_panic(NULL, "PAP-5720", "found direct item "
  1690. "%h or position (%d) does not match "
  1691. "to key %K", found_ih,
  1692. pos_in_item(path), key);
  1693. }
  1694. if (is_indirect_le_ih(found_ih)) {
  1695. if (le_ih_k_offset(found_ih) +
  1696. op_bytes_number(found_ih,
  1697. get_last_bh(path)->b_size) !=
  1698. cpu_key_k_offset(key)
  1699. || I_UNFM_NUM(found_ih) != pos_in_item(path)
  1700. || get_ih_free_space(found_ih) != 0)
  1701. reiserfs_panic(NULL, "PAP-5730", "found indirect "
  1702. "item (%h) or position (%d) does not "
  1703. "match to key (%K)",
  1704. found_ih, pos_in_item(path), key);
  1705. }
  1706. }
  1707. #endif /* config reiserfs check */
  1708. /* Paste bytes to the existing item. Returns bytes number pasted into the item. */
  1709. int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *search_path, /* Path to the pasted item. */
  1710. const struct cpu_key *key, /* Key to search for the needed item. */
  1711. struct inode *inode, /* Inode item belongs to */
  1712. const char *body, /* Pointer to the bytes to paste. */
  1713. int pasted_size)
  1714. { /* Size of pasted bytes. */
  1715. struct tree_balance s_paste_balance;
  1716. int retval;
  1717. int fs_gen;
  1718. BUG_ON(!th->t_trans_id);
  1719. fs_gen = get_generation(inode->i_sb);
  1720. #ifdef REISERQUOTA_DEBUG
  1721. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1722. "reiserquota paste_into_item(): allocating %u id=%u type=%c",
  1723. pasted_size, inode->i_uid,
  1724. key2type(&(key->on_disk_key)));
  1725. #endif
  1726. reiserfs_write_unlock(inode->i_sb);
  1727. retval = dquot_alloc_space_nodirty(inode, pasted_size);
  1728. reiserfs_write_lock(inode->i_sb);
  1729. if (retval) {
  1730. pathrelse(search_path);
  1731. return retval;
  1732. }
  1733. init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
  1734. pasted_size);
  1735. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1736. s_paste_balance.key = key->on_disk_key;
  1737. #endif
  1738. /* DQUOT_* can schedule, must check before the fix_nodes */
  1739. if (fs_changed(fs_gen, inode->i_sb)) {
  1740. goto search_again;
  1741. }
  1742. while ((retval =
  1743. fix_nodes(M_PASTE, &s_paste_balance, NULL,
  1744. body)) == REPEAT_SEARCH) {
  1745. search_again:
  1746. /* file system changed while we were in the fix_nodes */
  1747. PROC_INFO_INC(th->t_super, paste_into_item_restarted);
  1748. retval =
  1749. search_for_position_by_key(th->t_super, key,
  1750. search_path);
  1751. if (retval == IO_ERROR) {
  1752. retval = -EIO;
  1753. goto error_out;
  1754. }
  1755. if (retval == POSITION_FOUND) {
  1756. reiserfs_warning(inode->i_sb, "PAP-5710",
  1757. "entry or pasted byte (%K) exists",
  1758. key);
  1759. retval = -EEXIST;
  1760. goto error_out;
  1761. }
  1762. #ifdef CONFIG_REISERFS_CHECK
  1763. check_research_for_paste(search_path, key);
  1764. #endif
  1765. }
  1766. /* Perform balancing after all resources are collected by fix_nodes, and
  1767. accessing them will not risk triggering schedule. */
  1768. if (retval == CARRY_ON) {
  1769. do_balance(&s_paste_balance, NULL /*ih */ , body, M_PASTE);
  1770. return 0;
  1771. }
  1772. retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
  1773. error_out:
  1774. /* this also releases the path */
  1775. unfix_nodes(&s_paste_balance);
  1776. #ifdef REISERQUOTA_DEBUG
  1777. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1778. "reiserquota paste_into_item(): freeing %u id=%u type=%c",
  1779. pasted_size, inode->i_uid,
  1780. key2type(&(key->on_disk_key)));
  1781. #endif
  1782. dquot_free_space_nodirty(inode, pasted_size);
  1783. return retval;
  1784. }
  1785. /* Insert new item into the buffer at the path.
  1786. * th - active transaction handle
  1787. * path - path to the inserted item
  1788. * ih - pointer to the item header to insert
  1789. * body - pointer to the bytes to insert
  1790. */
  1791. int reiserfs_insert_item(struct reiserfs_transaction_handle *th,
  1792. struct treepath *path, const struct cpu_key *key,
  1793. struct item_head *ih, struct inode *inode,
  1794. const char *body)
  1795. {
  1796. struct tree_balance s_ins_balance;
  1797. int retval;
  1798. int fs_gen = 0;
  1799. int quota_bytes = 0;
  1800. BUG_ON(!th->t_trans_id);
  1801. if (inode) { /* Do we count quotas for item? */
  1802. fs_gen = get_generation(inode->i_sb);
  1803. quota_bytes = ih_item_len(ih);
  1804. /* hack so the quota code doesn't have to guess if the file has
  1805. ** a tail, links are always tails, so there's no guessing needed
  1806. */
  1807. if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(ih))
  1808. quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
  1809. #ifdef REISERQUOTA_DEBUG
  1810. reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
  1811. "reiserquota insert_item(): allocating %u id=%u type=%c",
  1812. quota_bytes, inode->i_uid, head2type(ih));
  1813. #endif
  1814. reiserfs_write_unlock(inode->i_sb);
  1815. /* We can't dirty inode here. It would be immediately written but
  1816. * appropriate stat item isn't inserted yet... */
  1817. retval = dquot_alloc_space_nodirty(inode, quota_bytes);
  1818. reiserfs_write_lock(inode->i_sb);
  1819. if (retval) {
  1820. pathrelse(path);
  1821. return retval;
  1822. }
  1823. }
  1824. init_tb_struct(th, &s_ins_balance, th->t_super, path,
  1825. IH_SIZE + ih_item_len(ih));
  1826. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1827. s_ins_balance.key = key->on_disk_key;
  1828. #endif
  1829. /* DQUOT_* can schedule, must check to be sure calling fix_nodes is safe */
  1830. if (inode && fs_changed(fs_gen, inode->i_sb)) {
  1831. goto search_again;
  1832. }
  1833. while ((retval =
  1834. fix_nodes(M_INSERT, &s_ins_balance, ih,
  1835. body)) == REPEAT_SEARCH) {
  1836. search_again:
  1837. /* file system changed while we were in the fix_nodes */
  1838. PROC_INFO_INC(th->t_super, insert_item_restarted);
  1839. retval = search_item(th->t_super, key, path);
  1840. if (retval == IO_ERROR) {
  1841. retval = -EIO;
  1842. goto error_out;
  1843. }
  1844. if (retval == ITEM_FOUND) {
  1845. reiserfs_warning(th->t_super, "PAP-5760",
  1846. "key %K already exists in the tree",
  1847. key);
  1848. retval = -EEXIST;
  1849. goto error_out;
  1850. }
  1851. }
  1852. /* make balancing after all resources will be collected at a time */
  1853. if (retval == CARRY_ON) {
  1854. do_balance(&s_ins_balance, ih, body, M_INSERT);
  1855. return 0;
  1856. }
  1857. retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
  1858. error_out:
  1859. /* also releases the path */
  1860. unfix_nodes(&s_ins_balance);
  1861. #ifdef REISERQUOTA_DEBUG
  1862. reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
  1863. "reiserquota insert_item(): freeing %u id=%u type=%c",
  1864. quota_bytes, inode->i_uid, head2type(ih));
  1865. #endif
  1866. if (inode)
  1867. dquot_free_space_nodirty(inode, quota_bytes);
  1868. return retval;
  1869. }