namei.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. *
  4. * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
  5. *
  6. * Trivial Changes:
  7. * Rights granted to Hans Reiser to redistribute under other terms providing
  8. * he accepts all liability including but not limited to patent, fitness
  9. * for purpose, and direct or indirect claims arising from failure to perform.
  10. *
  11. * NO WARRANTY
  12. */
  13. #include <linux/time.h>
  14. #include <linux/bitops.h>
  15. #include <linux/slab.h>
  16. #include "reiserfs.h"
  17. #include "acl.h"
  18. #include "xattr.h"
  19. #include <linux/quotaops.h>
  20. #define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) set_nlink(i, 1); }
  21. #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
  22. /*
  23. * directory item contains array of entry headers. This performs
  24. * binary search through that array
  25. */
  26. static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
  27. {
  28. struct item_head *ih = de->de_ih;
  29. struct reiserfs_de_head *deh = de->de_deh;
  30. int rbound, lbound, j;
  31. lbound = 0;
  32. rbound = ih_entry_count(ih) - 1;
  33. for (j = (rbound + lbound) / 2; lbound <= rbound;
  34. j = (rbound + lbound) / 2) {
  35. if (off < deh_offset(deh + j)) {
  36. rbound = j - 1;
  37. continue;
  38. }
  39. if (off > deh_offset(deh + j)) {
  40. lbound = j + 1;
  41. continue;
  42. }
  43. /* this is not name found, but matched third key component */
  44. de->de_entry_num = j;
  45. return NAME_FOUND;
  46. }
  47. de->de_entry_num = lbound;
  48. return NAME_NOT_FOUND;
  49. }
  50. /*
  51. * comment? maybe something like set de to point to what the path points to?
  52. */
  53. static inline void set_de_item_location(struct reiserfs_dir_entry *de,
  54. struct treepath *path)
  55. {
  56. de->de_bh = get_last_bh(path);
  57. de->de_ih = tp_item_head(path);
  58. de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
  59. de->de_item_num = PATH_LAST_POSITION(path);
  60. }
  61. /*
  62. * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
  63. */
  64. inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
  65. {
  66. struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
  67. BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
  68. de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
  69. de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
  70. de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
  71. if (de->de_name[de->de_namelen - 1] == 0)
  72. de->de_namelen = strlen(de->de_name);
  73. }
  74. /* what entry points to */
  75. static inline void set_de_object_key(struct reiserfs_dir_entry *de)
  76. {
  77. BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
  78. de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
  79. de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
  80. }
  81. static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
  82. {
  83. struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
  84. BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
  85. /* store key of the found entry */
  86. de->de_entry_key.version = KEY_FORMAT_3_5;
  87. de->de_entry_key.on_disk_key.k_dir_id =
  88. le32_to_cpu(de->de_ih->ih_key.k_dir_id);
  89. de->de_entry_key.on_disk_key.k_objectid =
  90. le32_to_cpu(de->de_ih->ih_key.k_objectid);
  91. set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
  92. set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
  93. }
  94. /*
  95. * We assign a key to each directory item, and place multiple entries in a
  96. * single directory item. A directory item has a key equal to the key of
  97. * the first directory entry in it.
  98. * This function first calls search_by_key, then, if item whose first entry
  99. * matches is not found it looks for the entry inside directory item found
  100. * by search_by_key. Fills the path to the entry, and to the entry position
  101. * in the item
  102. */
  103. /* The function is NOT SCHEDULE-SAFE! */
  104. int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
  105. struct treepath *path, struct reiserfs_dir_entry *de)
  106. {
  107. int retval;
  108. retval = search_item(sb, key, path);
  109. switch (retval) {
  110. case ITEM_NOT_FOUND:
  111. if (!PATH_LAST_POSITION(path)) {
  112. reiserfs_error(sb, "vs-7000", "search_by_key "
  113. "returned item position == 0");
  114. pathrelse(path);
  115. return IO_ERROR;
  116. }
  117. PATH_LAST_POSITION(path)--;
  118. case ITEM_FOUND:
  119. break;
  120. case IO_ERROR:
  121. return retval;
  122. default:
  123. pathrelse(path);
  124. reiserfs_error(sb, "vs-7002", "no path to here");
  125. return IO_ERROR;
  126. }
  127. set_de_item_location(de, path);
  128. #ifdef CONFIG_REISERFS_CHECK
  129. if (!is_direntry_le_ih(de->de_ih) ||
  130. COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
  131. print_block(de->de_bh, 0, -1, -1);
  132. reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
  133. "item or does not belong to the same directory "
  134. "as key %K", de->de_ih, key);
  135. }
  136. #endif /* CONFIG_REISERFS_CHECK */
  137. /*
  138. * binary search in directory item by third component of the
  139. * key. sets de->de_entry_num of de
  140. */
  141. retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
  142. path->pos_in_item = de->de_entry_num;
  143. if (retval != NAME_NOT_FOUND) {
  144. /*
  145. * ugly, but rename needs de_bh, de_deh, de_name,
  146. * de_namelen, de_objectid set
  147. */
  148. set_de_name_and_namelen(de);
  149. set_de_object_key(de);
  150. }
  151. return retval;
  152. }
  153. /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
  154. /*
  155. * The third component is hashed, and you can choose from more than
  156. * one hash function. Per directory hashes are not yet implemented
  157. * but are thought about. This function should be moved to hashes.c
  158. * Jedi, please do so. -Hans
  159. */
  160. static __u32 get_third_component(struct super_block *s,
  161. const char *name, int len)
  162. {
  163. __u32 res;
  164. if (!len || (len == 1 && name[0] == '.'))
  165. return DOT_OFFSET;
  166. if (len == 2 && name[0] == '.' && name[1] == '.')
  167. return DOT_DOT_OFFSET;
  168. res = REISERFS_SB(s)->s_hash_function(name, len);
  169. /* take bits from 7-th to 30-th including both bounds */
  170. res = GET_HASH_VALUE(res);
  171. if (res == 0)
  172. /*
  173. * needed to have no names before "." and ".." those have hash
  174. * value == 0 and generation conters 1 and 2 accordingly
  175. */
  176. res = 128;
  177. return res + MAX_GENERATION_NUMBER;
  178. }
  179. static int reiserfs_match(struct reiserfs_dir_entry *de,
  180. const char *name, int namelen)
  181. {
  182. int retval = NAME_NOT_FOUND;
  183. if ((namelen == de->de_namelen) &&
  184. !memcmp(de->de_name, name, de->de_namelen))
  185. retval =
  186. (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
  187. NAME_FOUND_INVISIBLE);
  188. return retval;
  189. }
  190. /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
  191. /* used when hash collisions exist */
  192. static int linear_search_in_dir_item(struct cpu_key *key,
  193. struct reiserfs_dir_entry *de,
  194. const char *name, int namelen)
  195. {
  196. struct reiserfs_de_head *deh = de->de_deh;
  197. int retval;
  198. int i;
  199. i = de->de_entry_num;
  200. if (i == ih_entry_count(de->de_ih) ||
  201. GET_HASH_VALUE(deh_offset(deh + i)) !=
  202. GET_HASH_VALUE(cpu_key_k_offset(key))) {
  203. i--;
  204. }
  205. RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
  206. "vs-7010: array of entry headers not found");
  207. deh += i;
  208. for (; i >= 0; i--, deh--) {
  209. /* hash value does not match, no need to check whole name */
  210. if (GET_HASH_VALUE(deh_offset(deh)) !=
  211. GET_HASH_VALUE(cpu_key_k_offset(key))) {
  212. return NAME_NOT_FOUND;
  213. }
  214. /* mark that this generation number is used */
  215. if (de->de_gen_number_bit_string)
  216. set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
  217. de->de_gen_number_bit_string);
  218. /* calculate pointer to name and namelen */
  219. de->de_entry_num = i;
  220. set_de_name_and_namelen(de);
  221. /*
  222. * de's de_name, de_namelen, de_recordlen are set.
  223. * Fill the rest.
  224. */
  225. if ((retval =
  226. reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
  227. /* key of pointed object */
  228. set_de_object_key(de);
  229. store_de_entry_key(de);
  230. /* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
  231. return retval;
  232. }
  233. }
  234. if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
  235. /*
  236. * we have reached left most entry in the node. In common we
  237. * have to go to the left neighbor, but if generation counter
  238. * is 0 already, we know for sure, that there is no name with
  239. * the same hash value
  240. */
  241. /*
  242. * FIXME: this work correctly only because hash value can not
  243. * be 0. Btw, in case of Yura's hash it is probably possible,
  244. * so, this is a bug
  245. */
  246. return NAME_NOT_FOUND;
  247. RFALSE(de->de_item_num,
  248. "vs-7015: two diritems of the same directory in one node?");
  249. return GOTO_PREVIOUS_ITEM;
  250. }
  251. /*
  252. * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
  253. * FIXME: should add something like IOERROR
  254. */
  255. static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
  256. struct treepath *path_to_entry,
  257. struct reiserfs_dir_entry *de)
  258. {
  259. struct cpu_key key_to_search;
  260. int retval;
  261. if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
  262. return NAME_NOT_FOUND;
  263. /* we will search for this key in the tree */
  264. make_cpu_key(&key_to_search, dir,
  265. get_third_component(dir->i_sb, name, namelen),
  266. TYPE_DIRENTRY, 3);
  267. while (1) {
  268. retval =
  269. search_by_entry_key(dir->i_sb, &key_to_search,
  270. path_to_entry, de);
  271. if (retval == IO_ERROR) {
  272. reiserfs_error(dir->i_sb, "zam-7001", "io error");
  273. return IO_ERROR;
  274. }
  275. /* compare names for all entries having given hash value */
  276. retval =
  277. linear_search_in_dir_item(&key_to_search, de, name,
  278. namelen);
  279. /*
  280. * there is no need to scan directory anymore.
  281. * Given entry found or does not exist
  282. */
  283. if (retval != GOTO_PREVIOUS_ITEM) {
  284. path_to_entry->pos_in_item = de->de_entry_num;
  285. return retval;
  286. }
  287. /*
  288. * there is left neighboring item of this directory
  289. * and given entry can be there
  290. */
  291. set_cpu_key_k_offset(&key_to_search,
  292. le_ih_k_offset(de->de_ih) - 1);
  293. pathrelse(path_to_entry);
  294. } /* while (1) */
  295. }
  296. static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
  297. unsigned int flags)
  298. {
  299. int retval;
  300. struct inode *inode = NULL;
  301. struct reiserfs_dir_entry de;
  302. INITIALIZE_PATH(path_to_entry);
  303. if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
  304. return ERR_PTR(-ENAMETOOLONG);
  305. reiserfs_write_lock(dir->i_sb);
  306. de.de_gen_number_bit_string = NULL;
  307. retval =
  308. reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
  309. &path_to_entry, &de);
  310. pathrelse(&path_to_entry);
  311. if (retval == NAME_FOUND) {
  312. inode = reiserfs_iget(dir->i_sb,
  313. (struct cpu_key *)&de.de_dir_id);
  314. if (!inode || IS_ERR(inode)) {
  315. reiserfs_write_unlock(dir->i_sb);
  316. return ERR_PTR(-EACCES);
  317. }
  318. /*
  319. * Propagate the private flag so we know we're
  320. * in the priv tree
  321. */
  322. if (IS_PRIVATE(dir))
  323. inode->i_flags |= S_PRIVATE;
  324. }
  325. reiserfs_write_unlock(dir->i_sb);
  326. if (retval == IO_ERROR) {
  327. return ERR_PTR(-EIO);
  328. }
  329. return d_splice_alias(inode, dentry);
  330. }
  331. /*
  332. * looks up the dentry of the parent directory for child.
  333. * taken from ext2_get_parent
  334. */
  335. struct dentry *reiserfs_get_parent(struct dentry *child)
  336. {
  337. int retval;
  338. struct inode *inode = NULL;
  339. struct reiserfs_dir_entry de;
  340. INITIALIZE_PATH(path_to_entry);
  341. struct inode *dir = d_inode(child);
  342. if (dir->i_nlink == 0) {
  343. return ERR_PTR(-ENOENT);
  344. }
  345. de.de_gen_number_bit_string = NULL;
  346. reiserfs_write_lock(dir->i_sb);
  347. retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
  348. pathrelse(&path_to_entry);
  349. if (retval != NAME_FOUND) {
  350. reiserfs_write_unlock(dir->i_sb);
  351. return ERR_PTR(-ENOENT);
  352. }
  353. inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
  354. reiserfs_write_unlock(dir->i_sb);
  355. return d_obtain_alias(inode);
  356. }
  357. /* add entry to the directory (entry can be hidden).
  358. insert definition of when hidden directories are used here -Hans
  359. Does not mark dir inode dirty, do it after successesfull call to it */
  360. static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
  361. struct inode *dir, const char *name, int namelen,
  362. struct inode *inode, int visible)
  363. {
  364. struct cpu_key entry_key;
  365. struct reiserfs_de_head *deh;
  366. INITIALIZE_PATH(path);
  367. struct reiserfs_dir_entry de;
  368. DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
  369. int gen_number;
  370. /*
  371. * 48 bytes now and we avoid kmalloc if we
  372. * create file with short name
  373. */
  374. char small_buf[32 + DEH_SIZE];
  375. char *buffer;
  376. int buflen, paste_size;
  377. int retval;
  378. BUG_ON(!th->t_trans_id);
  379. /* cannot allow items to be added into a busy deleted directory */
  380. if (!namelen)
  381. return -EINVAL;
  382. if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
  383. return -ENAMETOOLONG;
  384. /* each entry has unique key. compose it */
  385. make_cpu_key(&entry_key, dir,
  386. get_third_component(dir->i_sb, name, namelen),
  387. TYPE_DIRENTRY, 3);
  388. /* get memory for composing the entry */
  389. buflen = DEH_SIZE + ROUND_UP(namelen);
  390. if (buflen > sizeof(small_buf)) {
  391. buffer = kmalloc(buflen, GFP_NOFS);
  392. if (!buffer)
  393. return -ENOMEM;
  394. } else
  395. buffer = small_buf;
  396. paste_size =
  397. (get_inode_sd_version(dir) ==
  398. STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
  399. /*
  400. * fill buffer : directory entry head, name[, dir objectid | ,
  401. * stat data | ,stat data, dir objectid ]
  402. */
  403. deh = (struct reiserfs_de_head *)buffer;
  404. deh->deh_location = 0; /* JDM Endian safe if 0 */
  405. put_deh_offset(deh, cpu_key_k_offset(&entry_key));
  406. deh->deh_state = 0; /* JDM Endian safe if 0 */
  407. /* put key (ino analog) to de */
  408. /* safe: k_dir_id is le */
  409. deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
  410. /* safe: k_objectid is le */
  411. deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
  412. /* copy name */
  413. memcpy((char *)(deh + 1), name, namelen);
  414. /* padd by 0s to the 4 byte boundary */
  415. padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
  416. /*
  417. * entry is ready to be pasted into tree, set 'visibility'
  418. * and 'stat data in entry' attributes
  419. */
  420. mark_de_without_sd(deh);
  421. visible ? mark_de_visible(deh) : mark_de_hidden(deh);
  422. /* find the proper place for the new entry */
  423. memset(bit_string, 0, sizeof(bit_string));
  424. de.de_gen_number_bit_string = bit_string;
  425. retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
  426. if (retval != NAME_NOT_FOUND) {
  427. if (buffer != small_buf)
  428. kfree(buffer);
  429. pathrelse(&path);
  430. if (retval == IO_ERROR) {
  431. return -EIO;
  432. }
  433. if (retval != NAME_FOUND) {
  434. reiserfs_error(dir->i_sb, "zam-7002",
  435. "reiserfs_find_entry() returned "
  436. "unexpected value (%d)", retval);
  437. }
  438. return -EEXIST;
  439. }
  440. gen_number =
  441. find_first_zero_bit(bit_string,
  442. MAX_GENERATION_NUMBER + 1);
  443. if (gen_number > MAX_GENERATION_NUMBER) {
  444. /* there is no free generation number */
  445. reiserfs_warning(dir->i_sb, "reiserfs-7010",
  446. "Congratulations! we have got hash function "
  447. "screwed up");
  448. if (buffer != small_buf)
  449. kfree(buffer);
  450. pathrelse(&path);
  451. return -EBUSY;
  452. }
  453. /* adjust offset of directory enrty */
  454. put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
  455. set_cpu_key_k_offset(&entry_key, deh_offset(deh));
  456. /* update max-hash-collisions counter in reiserfs_sb_info */
  457. PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
  458. /* we need to re-search for the insertion point */
  459. if (gen_number != 0) {
  460. if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
  461. NAME_NOT_FOUND) {
  462. reiserfs_warning(dir->i_sb, "vs-7032",
  463. "entry with this key (%K) already "
  464. "exists", &entry_key);
  465. if (buffer != small_buf)
  466. kfree(buffer);
  467. pathrelse(&path);
  468. return -EBUSY;
  469. }
  470. }
  471. /* perform the insertion of the entry that we have prepared */
  472. retval =
  473. reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
  474. paste_size);
  475. if (buffer != small_buf)
  476. kfree(buffer);
  477. if (retval) {
  478. reiserfs_check_path(&path);
  479. return retval;
  480. }
  481. dir->i_size += paste_size;
  482. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  483. if (!S_ISDIR(inode->i_mode) && visible)
  484. /* reiserfs_mkdir or reiserfs_rename will do that by itself */
  485. reiserfs_update_sd(th, dir);
  486. reiserfs_check_path(&path);
  487. return 0;
  488. }
  489. /*
  490. * quota utility function, call if you've had to abort after calling
  491. * new_inode_init, and have not called reiserfs_new_inode yet.
  492. * This should only be called on inodes that do not have stat data
  493. * inserted into the tree yet.
  494. */
  495. static int drop_new_inode(struct inode *inode)
  496. {
  497. dquot_drop(inode);
  498. make_bad_inode(inode);
  499. inode->i_flags |= S_NOQUOTA;
  500. iput(inode);
  501. return 0;
  502. }
  503. /*
  504. * utility function that does setup for reiserfs_new_inode.
  505. * dquot_initialize needs lots of credits so it's better to have it
  506. * outside of a transaction, so we had to pull some bits of
  507. * reiserfs_new_inode out into this func.
  508. */
  509. static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
  510. {
  511. /*
  512. * Make inode invalid - just in case we are going to drop it before
  513. * the initialization happens
  514. */
  515. INODE_PKEY(inode)->k_objectid = 0;
  516. /*
  517. * the quota init calls have to know who to charge the quota to, so
  518. * we have to set uid and gid here
  519. */
  520. inode_init_owner(inode, dir, mode);
  521. dquot_initialize(inode);
  522. return 0;
  523. }
  524. static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  525. bool excl)
  526. {
  527. int retval;
  528. struct inode *inode;
  529. /*
  530. * We need blocks for transaction + (user+group)*(quotas
  531. * for new inode + update of quota for directory owner)
  532. */
  533. int jbegin_count =
  534. JOURNAL_PER_BALANCE_CNT * 2 +
  535. 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
  536. REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
  537. struct reiserfs_transaction_handle th;
  538. struct reiserfs_security_handle security;
  539. dquot_initialize(dir);
  540. if (!(inode = new_inode(dir->i_sb))) {
  541. return -ENOMEM;
  542. }
  543. new_inode_init(inode, dir, mode);
  544. jbegin_count += reiserfs_cache_default_acl(dir);
  545. retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
  546. if (retval < 0) {
  547. drop_new_inode(inode);
  548. return retval;
  549. }
  550. jbegin_count += retval;
  551. reiserfs_write_lock(dir->i_sb);
  552. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  553. if (retval) {
  554. drop_new_inode(inode);
  555. goto out_failed;
  556. }
  557. retval =
  558. reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
  559. inode, &security);
  560. if (retval)
  561. goto out_failed;
  562. inode->i_op = &reiserfs_file_inode_operations;
  563. inode->i_fop = &reiserfs_file_operations;
  564. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  565. retval =
  566. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  567. dentry->d_name.len, inode, 1 /*visible */ );
  568. if (retval) {
  569. int err;
  570. drop_nlink(inode);
  571. reiserfs_update_sd(&th, inode);
  572. err = journal_end(&th);
  573. if (err)
  574. retval = err;
  575. unlock_new_inode(inode);
  576. iput(inode);
  577. goto out_failed;
  578. }
  579. reiserfs_update_inode_transaction(inode);
  580. reiserfs_update_inode_transaction(dir);
  581. unlock_new_inode(inode);
  582. d_instantiate(dentry, inode);
  583. retval = journal_end(&th);
  584. out_failed:
  585. reiserfs_write_unlock(dir->i_sb);
  586. return retval;
  587. }
  588. static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  589. dev_t rdev)
  590. {
  591. int retval;
  592. struct inode *inode;
  593. struct reiserfs_transaction_handle th;
  594. struct reiserfs_security_handle security;
  595. /*
  596. * We need blocks for transaction + (user+group)*(quotas
  597. * for new inode + update of quota for directory owner)
  598. */
  599. int jbegin_count =
  600. JOURNAL_PER_BALANCE_CNT * 3 +
  601. 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
  602. REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
  603. if (!new_valid_dev(rdev))
  604. return -EINVAL;
  605. dquot_initialize(dir);
  606. if (!(inode = new_inode(dir->i_sb))) {
  607. return -ENOMEM;
  608. }
  609. new_inode_init(inode, dir, mode);
  610. jbegin_count += reiserfs_cache_default_acl(dir);
  611. retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
  612. if (retval < 0) {
  613. drop_new_inode(inode);
  614. return retval;
  615. }
  616. jbegin_count += retval;
  617. reiserfs_write_lock(dir->i_sb);
  618. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  619. if (retval) {
  620. drop_new_inode(inode);
  621. goto out_failed;
  622. }
  623. retval =
  624. reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
  625. inode, &security);
  626. if (retval) {
  627. goto out_failed;
  628. }
  629. inode->i_op = &reiserfs_special_inode_operations;
  630. init_special_inode(inode, inode->i_mode, rdev);
  631. /* FIXME: needed for block and char devices only */
  632. reiserfs_update_sd(&th, inode);
  633. reiserfs_update_inode_transaction(inode);
  634. reiserfs_update_inode_transaction(dir);
  635. retval =
  636. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  637. dentry->d_name.len, inode, 1 /*visible */ );
  638. if (retval) {
  639. int err;
  640. drop_nlink(inode);
  641. reiserfs_update_sd(&th, inode);
  642. err = journal_end(&th);
  643. if (err)
  644. retval = err;
  645. unlock_new_inode(inode);
  646. iput(inode);
  647. goto out_failed;
  648. }
  649. unlock_new_inode(inode);
  650. d_instantiate(dentry, inode);
  651. retval = journal_end(&th);
  652. out_failed:
  653. reiserfs_write_unlock(dir->i_sb);
  654. return retval;
  655. }
  656. static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  657. {
  658. int retval;
  659. struct inode *inode;
  660. struct reiserfs_transaction_handle th;
  661. struct reiserfs_security_handle security;
  662. /*
  663. * We need blocks for transaction + (user+group)*(quotas
  664. * for new inode + update of quota for directory owner)
  665. */
  666. int jbegin_count =
  667. JOURNAL_PER_BALANCE_CNT * 3 +
  668. 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
  669. REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
  670. dquot_initialize(dir);
  671. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  672. /*
  673. * set flag that new packing locality created and new blocks
  674. * for the content of that directory are not displaced yet
  675. */
  676. REISERFS_I(dir)->new_packing_locality = 1;
  677. #endif
  678. mode = S_IFDIR | mode;
  679. if (!(inode = new_inode(dir->i_sb))) {
  680. return -ENOMEM;
  681. }
  682. new_inode_init(inode, dir, mode);
  683. jbegin_count += reiserfs_cache_default_acl(dir);
  684. retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
  685. if (retval < 0) {
  686. drop_new_inode(inode);
  687. return retval;
  688. }
  689. jbegin_count += retval;
  690. reiserfs_write_lock(dir->i_sb);
  691. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  692. if (retval) {
  693. drop_new_inode(inode);
  694. goto out_failed;
  695. }
  696. /*
  697. * inc the link count now, so another writer doesn't overflow
  698. * it while we sleep later on.
  699. */
  700. INC_DIR_INODE_NLINK(dir)
  701. retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
  702. old_format_only(dir->i_sb) ?
  703. EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
  704. dentry, inode, &security);
  705. if (retval) {
  706. DEC_DIR_INODE_NLINK(dir)
  707. goto out_failed;
  708. }
  709. reiserfs_update_inode_transaction(inode);
  710. reiserfs_update_inode_transaction(dir);
  711. inode->i_op = &reiserfs_dir_inode_operations;
  712. inode->i_fop = &reiserfs_dir_operations;
  713. /* note, _this_ add_entry will not update dir's stat data */
  714. retval =
  715. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  716. dentry->d_name.len, inode, 1 /*visible */ );
  717. if (retval) {
  718. int err;
  719. clear_nlink(inode);
  720. DEC_DIR_INODE_NLINK(dir);
  721. reiserfs_update_sd(&th, inode);
  722. err = journal_end(&th);
  723. if (err)
  724. retval = err;
  725. unlock_new_inode(inode);
  726. iput(inode);
  727. goto out_failed;
  728. }
  729. /* the above add_entry did not update dir's stat data */
  730. reiserfs_update_sd(&th, dir);
  731. unlock_new_inode(inode);
  732. d_instantiate(dentry, inode);
  733. retval = journal_end(&th);
  734. out_failed:
  735. reiserfs_write_unlock(dir->i_sb);
  736. return retval;
  737. }
  738. static inline int reiserfs_empty_dir(struct inode *inode)
  739. {
  740. /*
  741. * we can cheat because an old format dir cannot have
  742. * EMPTY_DIR_SIZE, and a new format dir cannot have
  743. * EMPTY_DIR_SIZE_V1. So, if the inode is either size,
  744. * regardless of disk format version, the directory is empty.
  745. */
  746. if (inode->i_size != EMPTY_DIR_SIZE &&
  747. inode->i_size != EMPTY_DIR_SIZE_V1) {
  748. return 0;
  749. }
  750. return 1;
  751. }
  752. static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
  753. {
  754. int retval, err;
  755. struct inode *inode;
  756. struct reiserfs_transaction_handle th;
  757. int jbegin_count;
  758. INITIALIZE_PATH(path);
  759. struct reiserfs_dir_entry de;
  760. /*
  761. * we will be doing 2 balancings and update 2 stat data, we
  762. * change quotas of the owner of the directory and of the owner
  763. * of the parent directory. The quota structure is possibly
  764. * deleted only on last iput => outside of this transaction
  765. */
  766. jbegin_count =
  767. JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  768. 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
  769. dquot_initialize(dir);
  770. reiserfs_write_lock(dir->i_sb);
  771. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  772. if (retval)
  773. goto out_rmdir;
  774. de.de_gen_number_bit_string = NULL;
  775. if ((retval =
  776. reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
  777. &path, &de)) == NAME_NOT_FOUND) {
  778. retval = -ENOENT;
  779. goto end_rmdir;
  780. } else if (retval == IO_ERROR) {
  781. retval = -EIO;
  782. goto end_rmdir;
  783. }
  784. inode = d_inode(dentry);
  785. reiserfs_update_inode_transaction(inode);
  786. reiserfs_update_inode_transaction(dir);
  787. if (de.de_objectid != inode->i_ino) {
  788. /*
  789. * FIXME: compare key of an object and a key found in the entry
  790. */
  791. retval = -EIO;
  792. goto end_rmdir;
  793. }
  794. if (!reiserfs_empty_dir(inode)) {
  795. retval = -ENOTEMPTY;
  796. goto end_rmdir;
  797. }
  798. /* cut entry from dir directory */
  799. retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
  800. dir, NULL, /* page */
  801. 0 /*new file size - not used here */ );
  802. if (retval < 0)
  803. goto end_rmdir;
  804. if (inode->i_nlink != 2 && inode->i_nlink != 1)
  805. reiserfs_error(inode->i_sb, "reiserfs-7040",
  806. "empty directory has nlink != 2 (%d)",
  807. inode->i_nlink);
  808. clear_nlink(inode);
  809. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
  810. reiserfs_update_sd(&th, inode);
  811. DEC_DIR_INODE_NLINK(dir)
  812. dir->i_size -= (DEH_SIZE + de.de_entrylen);
  813. reiserfs_update_sd(&th, dir);
  814. /* prevent empty directory from getting lost */
  815. add_save_link(&th, inode, 0 /* not truncate */ );
  816. retval = journal_end(&th);
  817. reiserfs_check_path(&path);
  818. out_rmdir:
  819. reiserfs_write_unlock(dir->i_sb);
  820. return retval;
  821. end_rmdir:
  822. /*
  823. * we must release path, because we did not call
  824. * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
  825. * release path if operation was not complete
  826. */
  827. pathrelse(&path);
  828. err = journal_end(&th);
  829. reiserfs_write_unlock(dir->i_sb);
  830. return err ? err : retval;
  831. }
  832. static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
  833. {
  834. int retval, err;
  835. struct inode *inode;
  836. struct reiserfs_dir_entry de;
  837. INITIALIZE_PATH(path);
  838. struct reiserfs_transaction_handle th;
  839. int jbegin_count;
  840. unsigned long savelink;
  841. dquot_initialize(dir);
  842. inode = d_inode(dentry);
  843. /*
  844. * in this transaction we can be doing at max two balancings and
  845. * update two stat datas, we change quotas of the owner of the
  846. * directory and of the owner of the parent directory. The quota
  847. * structure is possibly deleted only on iput => outside of
  848. * this transaction
  849. */
  850. jbegin_count =
  851. JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  852. 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
  853. reiserfs_write_lock(dir->i_sb);
  854. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  855. if (retval)
  856. goto out_unlink;
  857. de.de_gen_number_bit_string = NULL;
  858. if ((retval =
  859. reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
  860. &path, &de)) == NAME_NOT_FOUND) {
  861. retval = -ENOENT;
  862. goto end_unlink;
  863. } else if (retval == IO_ERROR) {
  864. retval = -EIO;
  865. goto end_unlink;
  866. }
  867. reiserfs_update_inode_transaction(inode);
  868. reiserfs_update_inode_transaction(dir);
  869. if (de.de_objectid != inode->i_ino) {
  870. /*
  871. * FIXME: compare key of an object and a key found in the entry
  872. */
  873. retval = -EIO;
  874. goto end_unlink;
  875. }
  876. if (!inode->i_nlink) {
  877. reiserfs_warning(inode->i_sb, "reiserfs-7042",
  878. "deleting nonexistent file (%lu), %d",
  879. inode->i_ino, inode->i_nlink);
  880. set_nlink(inode, 1);
  881. }
  882. drop_nlink(inode);
  883. /*
  884. * we schedule before doing the add_save_link call, save the link
  885. * count so we don't race
  886. */
  887. savelink = inode->i_nlink;
  888. retval =
  889. reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
  890. 0);
  891. if (retval < 0) {
  892. inc_nlink(inode);
  893. goto end_unlink;
  894. }
  895. inode->i_ctime = CURRENT_TIME_SEC;
  896. reiserfs_update_sd(&th, inode);
  897. dir->i_size -= (de.de_entrylen + DEH_SIZE);
  898. dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
  899. reiserfs_update_sd(&th, dir);
  900. if (!savelink)
  901. /* prevent file from getting lost */
  902. add_save_link(&th, inode, 0 /* not truncate */ );
  903. retval = journal_end(&th);
  904. reiserfs_check_path(&path);
  905. reiserfs_write_unlock(dir->i_sb);
  906. return retval;
  907. end_unlink:
  908. pathrelse(&path);
  909. err = journal_end(&th);
  910. reiserfs_check_path(&path);
  911. if (err)
  912. retval = err;
  913. out_unlink:
  914. reiserfs_write_unlock(dir->i_sb);
  915. return retval;
  916. }
  917. static int reiserfs_symlink(struct inode *parent_dir,
  918. struct dentry *dentry, const char *symname)
  919. {
  920. int retval;
  921. struct inode *inode;
  922. char *name;
  923. int item_len;
  924. struct reiserfs_transaction_handle th;
  925. struct reiserfs_security_handle security;
  926. int mode = S_IFLNK | S_IRWXUGO;
  927. /*
  928. * We need blocks for transaction + (user+group)*(quotas for
  929. * new inode + update of quota for directory owner)
  930. */
  931. int jbegin_count =
  932. JOURNAL_PER_BALANCE_CNT * 3 +
  933. 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
  934. REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
  935. dquot_initialize(parent_dir);
  936. if (!(inode = new_inode(parent_dir->i_sb))) {
  937. return -ENOMEM;
  938. }
  939. new_inode_init(inode, parent_dir, mode);
  940. retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
  941. &security);
  942. if (retval < 0) {
  943. drop_new_inode(inode);
  944. return retval;
  945. }
  946. jbegin_count += retval;
  947. reiserfs_write_lock(parent_dir->i_sb);
  948. item_len = ROUND_UP(strlen(symname));
  949. if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
  950. retval = -ENAMETOOLONG;
  951. drop_new_inode(inode);
  952. goto out_failed;
  953. }
  954. name = kmalloc(item_len, GFP_NOFS);
  955. if (!name) {
  956. drop_new_inode(inode);
  957. retval = -ENOMEM;
  958. goto out_failed;
  959. }
  960. memcpy(name, symname, strlen(symname));
  961. padd_item(name, item_len, strlen(symname));
  962. retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
  963. if (retval) {
  964. drop_new_inode(inode);
  965. kfree(name);
  966. goto out_failed;
  967. }
  968. retval =
  969. reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
  970. dentry, inode, &security);
  971. kfree(name);
  972. if (retval) { /* reiserfs_new_inode iputs for us */
  973. goto out_failed;
  974. }
  975. reiserfs_update_inode_transaction(inode);
  976. reiserfs_update_inode_transaction(parent_dir);
  977. inode->i_op = &reiserfs_symlink_inode_operations;
  978. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  979. retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
  980. dentry->d_name.len, inode, 1 /*visible */ );
  981. if (retval) {
  982. int err;
  983. drop_nlink(inode);
  984. reiserfs_update_sd(&th, inode);
  985. err = journal_end(&th);
  986. if (err)
  987. retval = err;
  988. unlock_new_inode(inode);
  989. iput(inode);
  990. goto out_failed;
  991. }
  992. unlock_new_inode(inode);
  993. d_instantiate(dentry, inode);
  994. retval = journal_end(&th);
  995. out_failed:
  996. reiserfs_write_unlock(parent_dir->i_sb);
  997. return retval;
  998. }
  999. static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
  1000. struct dentry *dentry)
  1001. {
  1002. int retval;
  1003. struct inode *inode = d_inode(old_dentry);
  1004. struct reiserfs_transaction_handle th;
  1005. /*
  1006. * We need blocks for transaction + update of quotas for
  1007. * the owners of the directory
  1008. */
  1009. int jbegin_count =
  1010. JOURNAL_PER_BALANCE_CNT * 3 +
  1011. 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
  1012. dquot_initialize(dir);
  1013. reiserfs_write_lock(dir->i_sb);
  1014. if (inode->i_nlink >= REISERFS_LINK_MAX) {
  1015. /* FIXME: sd_nlink is 32 bit for new files */
  1016. reiserfs_write_unlock(dir->i_sb);
  1017. return -EMLINK;
  1018. }
  1019. /* inc before scheduling so reiserfs_unlink knows we are here */
  1020. inc_nlink(inode);
  1021. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  1022. if (retval) {
  1023. drop_nlink(inode);
  1024. reiserfs_write_unlock(dir->i_sb);
  1025. return retval;
  1026. }
  1027. /* create new entry */
  1028. retval =
  1029. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  1030. dentry->d_name.len, inode, 1 /*visible */ );
  1031. reiserfs_update_inode_transaction(inode);
  1032. reiserfs_update_inode_transaction(dir);
  1033. if (retval) {
  1034. int err;
  1035. drop_nlink(inode);
  1036. err = journal_end(&th);
  1037. reiserfs_write_unlock(dir->i_sb);
  1038. return err ? err : retval;
  1039. }
  1040. inode->i_ctime = CURRENT_TIME_SEC;
  1041. reiserfs_update_sd(&th, inode);
  1042. ihold(inode);
  1043. d_instantiate(dentry, inode);
  1044. retval = journal_end(&th);
  1045. reiserfs_write_unlock(dir->i_sb);
  1046. return retval;
  1047. }
  1048. /* de contains information pointing to an entry which */
  1049. static int de_still_valid(const char *name, int len,
  1050. struct reiserfs_dir_entry *de)
  1051. {
  1052. struct reiserfs_dir_entry tmp = *de;
  1053. /* recalculate pointer to name and name length */
  1054. set_de_name_and_namelen(&tmp);
  1055. /* FIXME: could check more */
  1056. if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
  1057. return 0;
  1058. return 1;
  1059. }
  1060. static int entry_points_to_object(const char *name, int len,
  1061. struct reiserfs_dir_entry *de,
  1062. struct inode *inode)
  1063. {
  1064. if (!de_still_valid(name, len, de))
  1065. return 0;
  1066. if (inode) {
  1067. if (!de_visible(de->de_deh + de->de_entry_num))
  1068. reiserfs_panic(inode->i_sb, "vs-7042",
  1069. "entry must be visible");
  1070. return (de->de_objectid == inode->i_ino) ? 1 : 0;
  1071. }
  1072. /* this must be added hidden entry */
  1073. if (de_visible(de->de_deh + de->de_entry_num))
  1074. reiserfs_panic(NULL, "vs-7043", "entry must be visible");
  1075. return 1;
  1076. }
  1077. /* sets key of objectid the entry has to point to */
  1078. static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
  1079. struct reiserfs_key *key)
  1080. {
  1081. /* JDM These operations are endian safe - both are le */
  1082. de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
  1083. de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
  1084. }
  1085. /*
  1086. * process, that is going to call fix_nodes/do_balance must hold only
  1087. * one path. If it holds 2 or more, it can get into endless waiting in
  1088. * get_empty_nodes or its clones
  1089. */
  1090. static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1091. struct inode *new_dir, struct dentry *new_dentry)
  1092. {
  1093. int retval;
  1094. INITIALIZE_PATH(old_entry_path);
  1095. INITIALIZE_PATH(new_entry_path);
  1096. INITIALIZE_PATH(dot_dot_entry_path);
  1097. struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
  1098. struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
  1099. struct inode *old_inode, *new_dentry_inode;
  1100. struct reiserfs_transaction_handle th;
  1101. int jbegin_count;
  1102. umode_t old_inode_mode;
  1103. unsigned long savelink = 1;
  1104. struct timespec ctime;
  1105. /*
  1106. * three balancings: (1) old name removal, (2) new name insertion
  1107. * and (3) maybe "save" link insertion
  1108. * stat data updates: (1) old directory,
  1109. * (2) new directory and (3) maybe old object stat data (when it is
  1110. * directory) and (4) maybe stat data of object to which new entry
  1111. * pointed initially and (5) maybe block containing ".." of
  1112. * renamed directory
  1113. * quota updates: two parent directories
  1114. */
  1115. jbegin_count =
  1116. JOURNAL_PER_BALANCE_CNT * 3 + 5 +
  1117. 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
  1118. dquot_initialize(old_dir);
  1119. dquot_initialize(new_dir);
  1120. old_inode = d_inode(old_dentry);
  1121. new_dentry_inode = d_inode(new_dentry);
  1122. /*
  1123. * make sure that oldname still exists and points to an object we
  1124. * are going to rename
  1125. */
  1126. old_de.de_gen_number_bit_string = NULL;
  1127. reiserfs_write_lock(old_dir->i_sb);
  1128. retval =
  1129. reiserfs_find_entry(old_dir, old_dentry->d_name.name,
  1130. old_dentry->d_name.len, &old_entry_path,
  1131. &old_de);
  1132. pathrelse(&old_entry_path);
  1133. if (retval == IO_ERROR) {
  1134. reiserfs_write_unlock(old_dir->i_sb);
  1135. return -EIO;
  1136. }
  1137. if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
  1138. reiserfs_write_unlock(old_dir->i_sb);
  1139. return -ENOENT;
  1140. }
  1141. old_inode_mode = old_inode->i_mode;
  1142. if (S_ISDIR(old_inode_mode)) {
  1143. /*
  1144. * make sure that directory being renamed has correct ".."
  1145. * and that its new parent directory has not too many links
  1146. * already
  1147. */
  1148. if (new_dentry_inode) {
  1149. if (!reiserfs_empty_dir(new_dentry_inode)) {
  1150. reiserfs_write_unlock(old_dir->i_sb);
  1151. return -ENOTEMPTY;
  1152. }
  1153. }
  1154. /*
  1155. * directory is renamed, its parent directory will be changed,
  1156. * so find ".." entry
  1157. */
  1158. dot_dot_de.de_gen_number_bit_string = NULL;
  1159. retval =
  1160. reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
  1161. &dot_dot_de);
  1162. pathrelse(&dot_dot_entry_path);
  1163. if (retval != NAME_FOUND) {
  1164. reiserfs_write_unlock(old_dir->i_sb);
  1165. return -EIO;
  1166. }
  1167. /* inode number of .. must equal old_dir->i_ino */
  1168. if (dot_dot_de.de_objectid != old_dir->i_ino) {
  1169. reiserfs_write_unlock(old_dir->i_sb);
  1170. return -EIO;
  1171. }
  1172. }
  1173. retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
  1174. if (retval) {
  1175. reiserfs_write_unlock(old_dir->i_sb);
  1176. return retval;
  1177. }
  1178. /* add new entry (or find the existing one) */
  1179. retval =
  1180. reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
  1181. new_dentry->d_name.len, old_inode, 0);
  1182. if (retval == -EEXIST) {
  1183. if (!new_dentry_inode) {
  1184. reiserfs_panic(old_dir->i_sb, "vs-7050",
  1185. "new entry is found, new inode == 0");
  1186. }
  1187. } else if (retval) {
  1188. int err = journal_end(&th);
  1189. reiserfs_write_unlock(old_dir->i_sb);
  1190. return err ? err : retval;
  1191. }
  1192. reiserfs_update_inode_transaction(old_dir);
  1193. reiserfs_update_inode_transaction(new_dir);
  1194. /*
  1195. * this makes it so an fsync on an open fd for the old name will
  1196. * commit the rename operation
  1197. */
  1198. reiserfs_update_inode_transaction(old_inode);
  1199. if (new_dentry_inode)
  1200. reiserfs_update_inode_transaction(new_dentry_inode);
  1201. while (1) {
  1202. /*
  1203. * look for old name using corresponding entry key
  1204. * (found by reiserfs_find_entry)
  1205. */
  1206. if ((retval =
  1207. search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
  1208. &old_entry_path,
  1209. &old_de)) != NAME_FOUND) {
  1210. pathrelse(&old_entry_path);
  1211. journal_end(&th);
  1212. reiserfs_write_unlock(old_dir->i_sb);
  1213. return -EIO;
  1214. }
  1215. copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
  1216. reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
  1217. /* look for new name by reiserfs_find_entry */
  1218. new_de.de_gen_number_bit_string = NULL;
  1219. retval =
  1220. reiserfs_find_entry(new_dir, new_dentry->d_name.name,
  1221. new_dentry->d_name.len, &new_entry_path,
  1222. &new_de);
  1223. /*
  1224. * reiserfs_add_entry should not return IO_ERROR,
  1225. * because it is called with essentially same parameters from
  1226. * reiserfs_add_entry above, and we'll catch any i/o errors
  1227. * before we get here.
  1228. */
  1229. if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
  1230. pathrelse(&new_entry_path);
  1231. pathrelse(&old_entry_path);
  1232. journal_end(&th);
  1233. reiserfs_write_unlock(old_dir->i_sb);
  1234. return -EIO;
  1235. }
  1236. copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
  1237. reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
  1238. if (S_ISDIR(old_inode->i_mode)) {
  1239. if ((retval =
  1240. search_by_entry_key(new_dir->i_sb,
  1241. &dot_dot_de.de_entry_key,
  1242. &dot_dot_entry_path,
  1243. &dot_dot_de)) != NAME_FOUND) {
  1244. pathrelse(&dot_dot_entry_path);
  1245. pathrelse(&new_entry_path);
  1246. pathrelse(&old_entry_path);
  1247. journal_end(&th);
  1248. reiserfs_write_unlock(old_dir->i_sb);
  1249. return -EIO;
  1250. }
  1251. copy_item_head(&dot_dot_ih,
  1252. tp_item_head(&dot_dot_entry_path));
  1253. /* node containing ".." gets into transaction */
  1254. reiserfs_prepare_for_journal(old_inode->i_sb,
  1255. dot_dot_de.de_bh, 1);
  1256. }
  1257. /*
  1258. * we should check seals here, not do
  1259. * this stuff, yes? Then, having
  1260. * gathered everything into RAM we
  1261. * should lock the buffers, yes? -Hans
  1262. */
  1263. /*
  1264. * probably. our rename needs to hold more
  1265. * than one path at once. The seals would
  1266. * have to be written to deal with multi-path
  1267. * issues -chris
  1268. */
  1269. /*
  1270. * sanity checking before doing the rename - avoid races many
  1271. * of the above checks could have scheduled. We have to be
  1272. * sure our items haven't been shifted by another process.
  1273. */
  1274. if (item_moved(&new_entry_ih, &new_entry_path) ||
  1275. !entry_points_to_object(new_dentry->d_name.name,
  1276. new_dentry->d_name.len,
  1277. &new_de, new_dentry_inode) ||
  1278. item_moved(&old_entry_ih, &old_entry_path) ||
  1279. !entry_points_to_object(old_dentry->d_name.name,
  1280. old_dentry->d_name.len,
  1281. &old_de, old_inode)) {
  1282. reiserfs_restore_prepared_buffer(old_inode->i_sb,
  1283. new_de.de_bh);
  1284. reiserfs_restore_prepared_buffer(old_inode->i_sb,
  1285. old_de.de_bh);
  1286. if (S_ISDIR(old_inode_mode))
  1287. reiserfs_restore_prepared_buffer(old_inode->
  1288. i_sb,
  1289. dot_dot_de.
  1290. de_bh);
  1291. continue;
  1292. }
  1293. if (S_ISDIR(old_inode_mode)) {
  1294. if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
  1295. !entry_points_to_object("..", 2, &dot_dot_de,
  1296. old_dir)) {
  1297. reiserfs_restore_prepared_buffer(old_inode->
  1298. i_sb,
  1299. old_de.de_bh);
  1300. reiserfs_restore_prepared_buffer(old_inode->
  1301. i_sb,
  1302. new_de.de_bh);
  1303. reiserfs_restore_prepared_buffer(old_inode->
  1304. i_sb,
  1305. dot_dot_de.
  1306. de_bh);
  1307. continue;
  1308. }
  1309. }
  1310. RFALSE(S_ISDIR(old_inode_mode) &&
  1311. !buffer_journal_prepared(dot_dot_de.de_bh), "");
  1312. break;
  1313. }
  1314. /*
  1315. * ok, all the changes can be done in one fell swoop when we
  1316. * have claimed all the buffers needed.
  1317. */
  1318. mark_de_visible(new_de.de_deh + new_de.de_entry_num);
  1319. set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
  1320. journal_mark_dirty(&th, new_de.de_bh);
  1321. mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
  1322. journal_mark_dirty(&th, old_de.de_bh);
  1323. ctime = CURRENT_TIME_SEC;
  1324. old_dir->i_ctime = old_dir->i_mtime = ctime;
  1325. new_dir->i_ctime = new_dir->i_mtime = ctime;
  1326. /*
  1327. * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
  1328. * which adds ctime update of renamed object
  1329. */
  1330. old_inode->i_ctime = ctime;
  1331. if (new_dentry_inode) {
  1332. /* adjust link number of the victim */
  1333. if (S_ISDIR(new_dentry_inode->i_mode)) {
  1334. clear_nlink(new_dentry_inode);
  1335. } else {
  1336. drop_nlink(new_dentry_inode);
  1337. }
  1338. new_dentry_inode->i_ctime = ctime;
  1339. savelink = new_dentry_inode->i_nlink;
  1340. }
  1341. if (S_ISDIR(old_inode_mode)) {
  1342. /* adjust ".." of renamed directory */
  1343. set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
  1344. journal_mark_dirty(&th, dot_dot_de.de_bh);
  1345. /*
  1346. * there (in new_dir) was no directory, so it got new link
  1347. * (".." of renamed directory)
  1348. */
  1349. if (!new_dentry_inode)
  1350. INC_DIR_INODE_NLINK(new_dir);
  1351. /* old directory lost one link - ".. " of renamed directory */
  1352. DEC_DIR_INODE_NLINK(old_dir);
  1353. }
  1354. /*
  1355. * looks like in 2.3.99pre3 brelse is atomic.
  1356. * so we can use pathrelse
  1357. */
  1358. pathrelse(&new_entry_path);
  1359. pathrelse(&dot_dot_entry_path);
  1360. /*
  1361. * FIXME: this reiserfs_cut_from_item's return value may screw up
  1362. * anybody, but it will panic if will not be able to find the
  1363. * entry. This needs one more clean up
  1364. */
  1365. if (reiserfs_cut_from_item
  1366. (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
  1367. 0) < 0)
  1368. reiserfs_error(old_dir->i_sb, "vs-7060",
  1369. "couldn't not cut old name. Fsck later?");
  1370. old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
  1371. reiserfs_update_sd(&th, old_dir);
  1372. reiserfs_update_sd(&th, new_dir);
  1373. reiserfs_update_sd(&th, old_inode);
  1374. if (new_dentry_inode) {
  1375. if (savelink == 0)
  1376. add_save_link(&th, new_dentry_inode,
  1377. 0 /* not truncate */ );
  1378. reiserfs_update_sd(&th, new_dentry_inode);
  1379. }
  1380. retval = journal_end(&th);
  1381. reiserfs_write_unlock(old_dir->i_sb);
  1382. return retval;
  1383. }
  1384. /* directories can handle most operations... */
  1385. const struct inode_operations reiserfs_dir_inode_operations = {
  1386. .create = reiserfs_create,
  1387. .lookup = reiserfs_lookup,
  1388. .link = reiserfs_link,
  1389. .unlink = reiserfs_unlink,
  1390. .symlink = reiserfs_symlink,
  1391. .mkdir = reiserfs_mkdir,
  1392. .rmdir = reiserfs_rmdir,
  1393. .mknod = reiserfs_mknod,
  1394. .rename = reiserfs_rename,
  1395. .setattr = reiserfs_setattr,
  1396. .setxattr = reiserfs_setxattr,
  1397. .getxattr = reiserfs_getxattr,
  1398. .listxattr = reiserfs_listxattr,
  1399. .removexattr = reiserfs_removexattr,
  1400. .permission = reiserfs_permission,
  1401. .get_acl = reiserfs_get_acl,
  1402. .set_acl = reiserfs_set_acl,
  1403. };
  1404. /*
  1405. * symlink operations.. same as page_symlink_inode_operations, with xattr
  1406. * stuff added
  1407. */
  1408. const struct inode_operations reiserfs_symlink_inode_operations = {
  1409. .readlink = generic_readlink,
  1410. .follow_link = page_follow_link_light,
  1411. .put_link = page_put_link,
  1412. .setattr = reiserfs_setattr,
  1413. .setxattr = reiserfs_setxattr,
  1414. .getxattr = reiserfs_getxattr,
  1415. .listxattr = reiserfs_listxattr,
  1416. .removexattr = reiserfs_removexattr,
  1417. .permission = reiserfs_permission,
  1418. };
  1419. /*
  1420. * special file operations.. just xattr/acl stuff
  1421. */
  1422. const struct inode_operations reiserfs_special_inode_operations = {
  1423. .setattr = reiserfs_setattr,
  1424. .setxattr = reiserfs_setxattr,
  1425. .getxattr = reiserfs_getxattr,
  1426. .listxattr = reiserfs_listxattr,
  1427. .removexattr = reiserfs_removexattr,
  1428. .permission = reiserfs_permission,
  1429. .get_acl = reiserfs_get_acl,
  1430. .set_acl = reiserfs_set_acl,
  1431. };