namei.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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(dir);
  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. return dquot_initialize(inode);
  522. }
  523. static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  524. bool excl)
  525. {
  526. int retval;
  527. struct inode *inode;
  528. /*
  529. * We need blocks for transaction + (user+group)*(quotas
  530. * for new inode + update of quota for directory owner)
  531. */
  532. int jbegin_count =
  533. JOURNAL_PER_BALANCE_CNT * 2 +
  534. 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
  535. REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
  536. struct reiserfs_transaction_handle th;
  537. struct reiserfs_security_handle security;
  538. retval = dquot_initialize(dir);
  539. if (retval)
  540. return retval;
  541. if (!(inode = new_inode(dir->i_sb))) {
  542. return -ENOMEM;
  543. }
  544. retval = new_inode_init(inode, dir, mode);
  545. if (retval) {
  546. drop_new_inode(inode);
  547. return retval;
  548. }
  549. jbegin_count += reiserfs_cache_default_acl(dir);
  550. retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
  551. if (retval < 0) {
  552. drop_new_inode(inode);
  553. return retval;
  554. }
  555. jbegin_count += retval;
  556. reiserfs_write_lock(dir->i_sb);
  557. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  558. if (retval) {
  559. drop_new_inode(inode);
  560. goto out_failed;
  561. }
  562. retval =
  563. reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
  564. inode, &security);
  565. if (retval)
  566. goto out_failed;
  567. inode->i_op = &reiserfs_file_inode_operations;
  568. inode->i_fop = &reiserfs_file_operations;
  569. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  570. retval =
  571. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  572. dentry->d_name.len, inode, 1 /*visible */ );
  573. if (retval) {
  574. int err;
  575. drop_nlink(inode);
  576. reiserfs_update_sd(&th, inode);
  577. err = journal_end(&th);
  578. if (err)
  579. retval = err;
  580. unlock_new_inode(inode);
  581. iput(inode);
  582. goto out_failed;
  583. }
  584. reiserfs_update_inode_transaction(inode);
  585. reiserfs_update_inode_transaction(dir);
  586. d_instantiate_new(dentry, inode);
  587. retval = journal_end(&th);
  588. out_failed:
  589. reiserfs_write_unlock(dir->i_sb);
  590. return retval;
  591. }
  592. static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  593. dev_t rdev)
  594. {
  595. int retval;
  596. struct inode *inode;
  597. struct reiserfs_transaction_handle th;
  598. struct reiserfs_security_handle security;
  599. /*
  600. * We need blocks for transaction + (user+group)*(quotas
  601. * for new inode + update of quota for directory owner)
  602. */
  603. int jbegin_count =
  604. JOURNAL_PER_BALANCE_CNT * 3 +
  605. 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
  606. REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
  607. retval = dquot_initialize(dir);
  608. if (retval)
  609. return retval;
  610. if (!(inode = new_inode(dir->i_sb))) {
  611. return -ENOMEM;
  612. }
  613. retval = new_inode_init(inode, dir, mode);
  614. if (retval) {
  615. drop_new_inode(inode);
  616. return retval;
  617. }
  618. jbegin_count += reiserfs_cache_default_acl(dir);
  619. retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
  620. if (retval < 0) {
  621. drop_new_inode(inode);
  622. return retval;
  623. }
  624. jbegin_count += retval;
  625. reiserfs_write_lock(dir->i_sb);
  626. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  627. if (retval) {
  628. drop_new_inode(inode);
  629. goto out_failed;
  630. }
  631. retval =
  632. reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
  633. inode, &security);
  634. if (retval) {
  635. goto out_failed;
  636. }
  637. inode->i_op = &reiserfs_special_inode_operations;
  638. init_special_inode(inode, inode->i_mode, rdev);
  639. /* FIXME: needed for block and char devices only */
  640. reiserfs_update_sd(&th, inode);
  641. reiserfs_update_inode_transaction(inode);
  642. reiserfs_update_inode_transaction(dir);
  643. retval =
  644. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  645. dentry->d_name.len, inode, 1 /*visible */ );
  646. if (retval) {
  647. int err;
  648. drop_nlink(inode);
  649. reiserfs_update_sd(&th, inode);
  650. err = journal_end(&th);
  651. if (err)
  652. retval = err;
  653. unlock_new_inode(inode);
  654. iput(inode);
  655. goto out_failed;
  656. }
  657. d_instantiate_new(dentry, inode);
  658. retval = journal_end(&th);
  659. out_failed:
  660. reiserfs_write_unlock(dir->i_sb);
  661. return retval;
  662. }
  663. static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  664. {
  665. int retval;
  666. struct inode *inode;
  667. struct reiserfs_transaction_handle th;
  668. struct reiserfs_security_handle security;
  669. /*
  670. * We need blocks for transaction + (user+group)*(quotas
  671. * for new inode + update of quota for directory owner)
  672. */
  673. int jbegin_count =
  674. JOURNAL_PER_BALANCE_CNT * 3 +
  675. 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
  676. REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
  677. retval = dquot_initialize(dir);
  678. if (retval)
  679. return retval;
  680. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  681. /*
  682. * set flag that new packing locality created and new blocks
  683. * for the content of that directory are not displaced yet
  684. */
  685. REISERFS_I(dir)->new_packing_locality = 1;
  686. #endif
  687. mode = S_IFDIR | mode;
  688. if (!(inode = new_inode(dir->i_sb))) {
  689. return -ENOMEM;
  690. }
  691. retval = new_inode_init(inode, dir, mode);
  692. if (retval) {
  693. drop_new_inode(inode);
  694. return retval;
  695. }
  696. jbegin_count += reiserfs_cache_default_acl(dir);
  697. retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
  698. if (retval < 0) {
  699. drop_new_inode(inode);
  700. return retval;
  701. }
  702. jbegin_count += retval;
  703. reiserfs_write_lock(dir->i_sb);
  704. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  705. if (retval) {
  706. drop_new_inode(inode);
  707. goto out_failed;
  708. }
  709. /*
  710. * inc the link count now, so another writer doesn't overflow
  711. * it while we sleep later on.
  712. */
  713. INC_DIR_INODE_NLINK(dir)
  714. retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
  715. old_format_only(dir->i_sb) ?
  716. EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
  717. dentry, inode, &security);
  718. if (retval) {
  719. DEC_DIR_INODE_NLINK(dir)
  720. goto out_failed;
  721. }
  722. reiserfs_update_inode_transaction(inode);
  723. reiserfs_update_inode_transaction(dir);
  724. inode->i_op = &reiserfs_dir_inode_operations;
  725. inode->i_fop = &reiserfs_dir_operations;
  726. /* note, _this_ add_entry will not update dir's stat data */
  727. retval =
  728. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  729. dentry->d_name.len, inode, 1 /*visible */ );
  730. if (retval) {
  731. int err;
  732. clear_nlink(inode);
  733. DEC_DIR_INODE_NLINK(dir);
  734. reiserfs_update_sd(&th, inode);
  735. err = journal_end(&th);
  736. if (err)
  737. retval = err;
  738. unlock_new_inode(inode);
  739. iput(inode);
  740. goto out_failed;
  741. }
  742. /* the above add_entry did not update dir's stat data */
  743. reiserfs_update_sd(&th, dir);
  744. d_instantiate_new(dentry, inode);
  745. retval = journal_end(&th);
  746. out_failed:
  747. reiserfs_write_unlock(dir->i_sb);
  748. return retval;
  749. }
  750. static inline int reiserfs_empty_dir(struct inode *inode)
  751. {
  752. /*
  753. * we can cheat because an old format dir cannot have
  754. * EMPTY_DIR_SIZE, and a new format dir cannot have
  755. * EMPTY_DIR_SIZE_V1. So, if the inode is either size,
  756. * regardless of disk format version, the directory is empty.
  757. */
  758. if (inode->i_size != EMPTY_DIR_SIZE &&
  759. inode->i_size != EMPTY_DIR_SIZE_V1) {
  760. return 0;
  761. }
  762. return 1;
  763. }
  764. static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
  765. {
  766. int retval, err;
  767. struct inode *inode;
  768. struct reiserfs_transaction_handle th;
  769. int jbegin_count;
  770. INITIALIZE_PATH(path);
  771. struct reiserfs_dir_entry de;
  772. /*
  773. * we will be doing 2 balancings and update 2 stat data, we
  774. * change quotas of the owner of the directory and of the owner
  775. * of the parent directory. The quota structure is possibly
  776. * deleted only on last iput => outside of this transaction
  777. */
  778. jbegin_count =
  779. JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  780. 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
  781. retval = dquot_initialize(dir);
  782. if (retval)
  783. return retval;
  784. reiserfs_write_lock(dir->i_sb);
  785. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  786. if (retval)
  787. goto out_rmdir;
  788. de.de_gen_number_bit_string = NULL;
  789. if ((retval =
  790. reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
  791. &path, &de)) == NAME_NOT_FOUND) {
  792. retval = -ENOENT;
  793. goto end_rmdir;
  794. } else if (retval == IO_ERROR) {
  795. retval = -EIO;
  796. goto end_rmdir;
  797. }
  798. inode = d_inode(dentry);
  799. reiserfs_update_inode_transaction(inode);
  800. reiserfs_update_inode_transaction(dir);
  801. if (de.de_objectid != inode->i_ino) {
  802. /*
  803. * FIXME: compare key of an object and a key found in the entry
  804. */
  805. retval = -EIO;
  806. goto end_rmdir;
  807. }
  808. if (!reiserfs_empty_dir(inode)) {
  809. retval = -ENOTEMPTY;
  810. goto end_rmdir;
  811. }
  812. /* cut entry from dir directory */
  813. retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
  814. dir, NULL, /* page */
  815. 0 /*new file size - not used here */ );
  816. if (retval < 0)
  817. goto end_rmdir;
  818. if (inode->i_nlink != 2 && inode->i_nlink != 1)
  819. reiserfs_error(inode->i_sb, "reiserfs-7040",
  820. "empty directory has nlink != 2 (%d)",
  821. inode->i_nlink);
  822. clear_nlink(inode);
  823. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
  824. reiserfs_update_sd(&th, inode);
  825. DEC_DIR_INODE_NLINK(dir)
  826. dir->i_size -= (DEH_SIZE + de.de_entrylen);
  827. reiserfs_update_sd(&th, dir);
  828. /* prevent empty directory from getting lost */
  829. add_save_link(&th, inode, 0 /* not truncate */ );
  830. retval = journal_end(&th);
  831. reiserfs_check_path(&path);
  832. out_rmdir:
  833. reiserfs_write_unlock(dir->i_sb);
  834. return retval;
  835. end_rmdir:
  836. /*
  837. * we must release path, because we did not call
  838. * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
  839. * release path if operation was not complete
  840. */
  841. pathrelse(&path);
  842. err = journal_end(&th);
  843. reiserfs_write_unlock(dir->i_sb);
  844. return err ? err : retval;
  845. }
  846. static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
  847. {
  848. int retval, err;
  849. struct inode *inode;
  850. struct reiserfs_dir_entry de;
  851. INITIALIZE_PATH(path);
  852. struct reiserfs_transaction_handle th;
  853. int jbegin_count;
  854. unsigned long savelink;
  855. retval = dquot_initialize(dir);
  856. if (retval)
  857. return retval;
  858. inode = d_inode(dentry);
  859. /*
  860. * in this transaction we can be doing at max two balancings and
  861. * update two stat datas, we change quotas of the owner of the
  862. * directory and of the owner of the parent directory. The quota
  863. * structure is possibly deleted only on iput => outside of
  864. * this transaction
  865. */
  866. jbegin_count =
  867. JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  868. 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
  869. reiserfs_write_lock(dir->i_sb);
  870. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  871. if (retval)
  872. goto out_unlink;
  873. de.de_gen_number_bit_string = NULL;
  874. if ((retval =
  875. reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
  876. &path, &de)) == NAME_NOT_FOUND) {
  877. retval = -ENOENT;
  878. goto end_unlink;
  879. } else if (retval == IO_ERROR) {
  880. retval = -EIO;
  881. goto end_unlink;
  882. }
  883. reiserfs_update_inode_transaction(inode);
  884. reiserfs_update_inode_transaction(dir);
  885. if (de.de_objectid != inode->i_ino) {
  886. /*
  887. * FIXME: compare key of an object and a key found in the entry
  888. */
  889. retval = -EIO;
  890. goto end_unlink;
  891. }
  892. if (!inode->i_nlink) {
  893. reiserfs_warning(inode->i_sb, "reiserfs-7042",
  894. "deleting nonexistent file (%lu), %d",
  895. inode->i_ino, inode->i_nlink);
  896. set_nlink(inode, 1);
  897. }
  898. drop_nlink(inode);
  899. /*
  900. * we schedule before doing the add_save_link call, save the link
  901. * count so we don't race
  902. */
  903. savelink = inode->i_nlink;
  904. retval =
  905. reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
  906. 0);
  907. if (retval < 0) {
  908. inc_nlink(inode);
  909. goto end_unlink;
  910. }
  911. inode->i_ctime = current_time(inode);
  912. reiserfs_update_sd(&th, inode);
  913. dir->i_size -= (de.de_entrylen + DEH_SIZE);
  914. dir->i_ctime = dir->i_mtime = current_time(dir);
  915. reiserfs_update_sd(&th, dir);
  916. if (!savelink)
  917. /* prevent file from getting lost */
  918. add_save_link(&th, inode, 0 /* not truncate */ );
  919. retval = journal_end(&th);
  920. reiserfs_check_path(&path);
  921. reiserfs_write_unlock(dir->i_sb);
  922. return retval;
  923. end_unlink:
  924. pathrelse(&path);
  925. err = journal_end(&th);
  926. reiserfs_check_path(&path);
  927. if (err)
  928. retval = err;
  929. out_unlink:
  930. reiserfs_write_unlock(dir->i_sb);
  931. return retval;
  932. }
  933. static int reiserfs_symlink(struct inode *parent_dir,
  934. struct dentry *dentry, const char *symname)
  935. {
  936. int retval;
  937. struct inode *inode;
  938. char *name;
  939. int item_len;
  940. struct reiserfs_transaction_handle th;
  941. struct reiserfs_security_handle security;
  942. int mode = S_IFLNK | S_IRWXUGO;
  943. /*
  944. * We need blocks for transaction + (user+group)*(quotas for
  945. * new inode + update of quota for directory owner)
  946. */
  947. int jbegin_count =
  948. JOURNAL_PER_BALANCE_CNT * 3 +
  949. 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
  950. REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
  951. retval = dquot_initialize(parent_dir);
  952. if (retval)
  953. return retval;
  954. if (!(inode = new_inode(parent_dir->i_sb))) {
  955. return -ENOMEM;
  956. }
  957. retval = new_inode_init(inode, parent_dir, mode);
  958. if (retval) {
  959. drop_new_inode(inode);
  960. return retval;
  961. }
  962. retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
  963. &security);
  964. if (retval < 0) {
  965. drop_new_inode(inode);
  966. return retval;
  967. }
  968. jbegin_count += retval;
  969. reiserfs_write_lock(parent_dir->i_sb);
  970. item_len = ROUND_UP(strlen(symname));
  971. if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
  972. retval = -ENAMETOOLONG;
  973. drop_new_inode(inode);
  974. goto out_failed;
  975. }
  976. name = kmalloc(item_len, GFP_NOFS);
  977. if (!name) {
  978. drop_new_inode(inode);
  979. retval = -ENOMEM;
  980. goto out_failed;
  981. }
  982. memcpy(name, symname, strlen(symname));
  983. padd_item(name, item_len, strlen(symname));
  984. retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
  985. if (retval) {
  986. drop_new_inode(inode);
  987. kfree(name);
  988. goto out_failed;
  989. }
  990. retval =
  991. reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
  992. dentry, inode, &security);
  993. kfree(name);
  994. if (retval) { /* reiserfs_new_inode iputs for us */
  995. goto out_failed;
  996. }
  997. reiserfs_update_inode_transaction(inode);
  998. reiserfs_update_inode_transaction(parent_dir);
  999. inode->i_op = &reiserfs_symlink_inode_operations;
  1000. inode_nohighmem(inode);
  1001. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  1002. retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
  1003. dentry->d_name.len, inode, 1 /*visible */ );
  1004. if (retval) {
  1005. int err;
  1006. drop_nlink(inode);
  1007. reiserfs_update_sd(&th, inode);
  1008. err = journal_end(&th);
  1009. if (err)
  1010. retval = err;
  1011. unlock_new_inode(inode);
  1012. iput(inode);
  1013. goto out_failed;
  1014. }
  1015. d_instantiate_new(dentry, inode);
  1016. retval = journal_end(&th);
  1017. out_failed:
  1018. reiserfs_write_unlock(parent_dir->i_sb);
  1019. return retval;
  1020. }
  1021. static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
  1022. struct dentry *dentry)
  1023. {
  1024. int retval;
  1025. struct inode *inode = d_inode(old_dentry);
  1026. struct reiserfs_transaction_handle th;
  1027. /*
  1028. * We need blocks for transaction + update of quotas for
  1029. * the owners of the directory
  1030. */
  1031. int jbegin_count =
  1032. JOURNAL_PER_BALANCE_CNT * 3 +
  1033. 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
  1034. retval = dquot_initialize(dir);
  1035. if (retval)
  1036. return retval;
  1037. reiserfs_write_lock(dir->i_sb);
  1038. if (inode->i_nlink >= REISERFS_LINK_MAX) {
  1039. /* FIXME: sd_nlink is 32 bit for new files */
  1040. reiserfs_write_unlock(dir->i_sb);
  1041. return -EMLINK;
  1042. }
  1043. /* inc before scheduling so reiserfs_unlink knows we are here */
  1044. inc_nlink(inode);
  1045. retval = journal_begin(&th, dir->i_sb, jbegin_count);
  1046. if (retval) {
  1047. drop_nlink(inode);
  1048. reiserfs_write_unlock(dir->i_sb);
  1049. return retval;
  1050. }
  1051. /* create new entry */
  1052. retval =
  1053. reiserfs_add_entry(&th, dir, dentry->d_name.name,
  1054. dentry->d_name.len, inode, 1 /*visible */ );
  1055. reiserfs_update_inode_transaction(inode);
  1056. reiserfs_update_inode_transaction(dir);
  1057. if (retval) {
  1058. int err;
  1059. drop_nlink(inode);
  1060. err = journal_end(&th);
  1061. reiserfs_write_unlock(dir->i_sb);
  1062. return err ? err : retval;
  1063. }
  1064. inode->i_ctime = current_time(inode);
  1065. reiserfs_update_sd(&th, inode);
  1066. ihold(inode);
  1067. d_instantiate(dentry, inode);
  1068. retval = journal_end(&th);
  1069. reiserfs_write_unlock(dir->i_sb);
  1070. return retval;
  1071. }
  1072. /* de contains information pointing to an entry which */
  1073. static int de_still_valid(const char *name, int len,
  1074. struct reiserfs_dir_entry *de)
  1075. {
  1076. struct reiserfs_dir_entry tmp = *de;
  1077. /* recalculate pointer to name and name length */
  1078. set_de_name_and_namelen(&tmp);
  1079. /* FIXME: could check more */
  1080. if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
  1081. return 0;
  1082. return 1;
  1083. }
  1084. static int entry_points_to_object(const char *name, int len,
  1085. struct reiserfs_dir_entry *de,
  1086. struct inode *inode)
  1087. {
  1088. if (!de_still_valid(name, len, de))
  1089. return 0;
  1090. if (inode) {
  1091. if (!de_visible(de->de_deh + de->de_entry_num))
  1092. reiserfs_panic(inode->i_sb, "vs-7042",
  1093. "entry must be visible");
  1094. return (de->de_objectid == inode->i_ino) ? 1 : 0;
  1095. }
  1096. /* this must be added hidden entry */
  1097. if (de_visible(de->de_deh + de->de_entry_num))
  1098. reiserfs_panic(NULL, "vs-7043", "entry must be visible");
  1099. return 1;
  1100. }
  1101. /* sets key of objectid the entry has to point to */
  1102. static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
  1103. struct reiserfs_key *key)
  1104. {
  1105. /* JDM These operations are endian safe - both are le */
  1106. de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
  1107. de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
  1108. }
  1109. /*
  1110. * process, that is going to call fix_nodes/do_balance must hold only
  1111. * one path. If it holds 2 or more, it can get into endless waiting in
  1112. * get_empty_nodes or its clones
  1113. */
  1114. static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1115. struct inode *new_dir, struct dentry *new_dentry,
  1116. unsigned int flags)
  1117. {
  1118. int retval;
  1119. INITIALIZE_PATH(old_entry_path);
  1120. INITIALIZE_PATH(new_entry_path);
  1121. INITIALIZE_PATH(dot_dot_entry_path);
  1122. struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
  1123. struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
  1124. struct inode *old_inode, *new_dentry_inode;
  1125. struct reiserfs_transaction_handle th;
  1126. int jbegin_count;
  1127. umode_t old_inode_mode;
  1128. unsigned long savelink = 1;
  1129. struct timespec ctime;
  1130. if (flags & ~RENAME_NOREPLACE)
  1131. return -EINVAL;
  1132. /*
  1133. * three balancings: (1) old name removal, (2) new name insertion
  1134. * and (3) maybe "save" link insertion
  1135. * stat data updates: (1) old directory,
  1136. * (2) new directory and (3) maybe old object stat data (when it is
  1137. * directory) and (4) maybe stat data of object to which new entry
  1138. * pointed initially and (5) maybe block containing ".." of
  1139. * renamed directory
  1140. * quota updates: two parent directories
  1141. */
  1142. jbegin_count =
  1143. JOURNAL_PER_BALANCE_CNT * 3 + 5 +
  1144. 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
  1145. retval = dquot_initialize(old_dir);
  1146. if (retval)
  1147. return retval;
  1148. retval = dquot_initialize(new_dir);
  1149. if (retval)
  1150. return retval;
  1151. old_inode = d_inode(old_dentry);
  1152. new_dentry_inode = d_inode(new_dentry);
  1153. /*
  1154. * make sure that oldname still exists and points to an object we
  1155. * are going to rename
  1156. */
  1157. old_de.de_gen_number_bit_string = NULL;
  1158. reiserfs_write_lock(old_dir->i_sb);
  1159. retval =
  1160. reiserfs_find_entry(old_dir, old_dentry->d_name.name,
  1161. old_dentry->d_name.len, &old_entry_path,
  1162. &old_de);
  1163. pathrelse(&old_entry_path);
  1164. if (retval == IO_ERROR) {
  1165. reiserfs_write_unlock(old_dir->i_sb);
  1166. return -EIO;
  1167. }
  1168. if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
  1169. reiserfs_write_unlock(old_dir->i_sb);
  1170. return -ENOENT;
  1171. }
  1172. old_inode_mode = old_inode->i_mode;
  1173. if (S_ISDIR(old_inode_mode)) {
  1174. /*
  1175. * make sure that directory being renamed has correct ".."
  1176. * and that its new parent directory has not too many links
  1177. * already
  1178. */
  1179. if (new_dentry_inode) {
  1180. if (!reiserfs_empty_dir(new_dentry_inode)) {
  1181. reiserfs_write_unlock(old_dir->i_sb);
  1182. return -ENOTEMPTY;
  1183. }
  1184. }
  1185. /*
  1186. * directory is renamed, its parent directory will be changed,
  1187. * so find ".." entry
  1188. */
  1189. dot_dot_de.de_gen_number_bit_string = NULL;
  1190. retval =
  1191. reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
  1192. &dot_dot_de);
  1193. pathrelse(&dot_dot_entry_path);
  1194. if (retval != NAME_FOUND) {
  1195. reiserfs_write_unlock(old_dir->i_sb);
  1196. return -EIO;
  1197. }
  1198. /* inode number of .. must equal old_dir->i_ino */
  1199. if (dot_dot_de.de_objectid != old_dir->i_ino) {
  1200. reiserfs_write_unlock(old_dir->i_sb);
  1201. return -EIO;
  1202. }
  1203. }
  1204. retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
  1205. if (retval) {
  1206. reiserfs_write_unlock(old_dir->i_sb);
  1207. return retval;
  1208. }
  1209. /* add new entry (or find the existing one) */
  1210. retval =
  1211. reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
  1212. new_dentry->d_name.len, old_inode, 0);
  1213. if (retval == -EEXIST) {
  1214. if (!new_dentry_inode) {
  1215. reiserfs_panic(old_dir->i_sb, "vs-7050",
  1216. "new entry is found, new inode == 0");
  1217. }
  1218. } else if (retval) {
  1219. int err = journal_end(&th);
  1220. reiserfs_write_unlock(old_dir->i_sb);
  1221. return err ? err : retval;
  1222. }
  1223. reiserfs_update_inode_transaction(old_dir);
  1224. reiserfs_update_inode_transaction(new_dir);
  1225. /*
  1226. * this makes it so an fsync on an open fd for the old name will
  1227. * commit the rename operation
  1228. */
  1229. reiserfs_update_inode_transaction(old_inode);
  1230. if (new_dentry_inode)
  1231. reiserfs_update_inode_transaction(new_dentry_inode);
  1232. while (1) {
  1233. /*
  1234. * look for old name using corresponding entry key
  1235. * (found by reiserfs_find_entry)
  1236. */
  1237. if ((retval =
  1238. search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
  1239. &old_entry_path,
  1240. &old_de)) != NAME_FOUND) {
  1241. pathrelse(&old_entry_path);
  1242. journal_end(&th);
  1243. reiserfs_write_unlock(old_dir->i_sb);
  1244. return -EIO;
  1245. }
  1246. copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
  1247. reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
  1248. /* look for new name by reiserfs_find_entry */
  1249. new_de.de_gen_number_bit_string = NULL;
  1250. retval =
  1251. reiserfs_find_entry(new_dir, new_dentry->d_name.name,
  1252. new_dentry->d_name.len, &new_entry_path,
  1253. &new_de);
  1254. /*
  1255. * reiserfs_add_entry should not return IO_ERROR,
  1256. * because it is called with essentially same parameters from
  1257. * reiserfs_add_entry above, and we'll catch any i/o errors
  1258. * before we get here.
  1259. */
  1260. if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
  1261. pathrelse(&new_entry_path);
  1262. pathrelse(&old_entry_path);
  1263. journal_end(&th);
  1264. reiserfs_write_unlock(old_dir->i_sb);
  1265. return -EIO;
  1266. }
  1267. copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
  1268. reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
  1269. if (S_ISDIR(old_inode->i_mode)) {
  1270. if ((retval =
  1271. search_by_entry_key(new_dir->i_sb,
  1272. &dot_dot_de.de_entry_key,
  1273. &dot_dot_entry_path,
  1274. &dot_dot_de)) != NAME_FOUND) {
  1275. pathrelse(&dot_dot_entry_path);
  1276. pathrelse(&new_entry_path);
  1277. pathrelse(&old_entry_path);
  1278. journal_end(&th);
  1279. reiserfs_write_unlock(old_dir->i_sb);
  1280. return -EIO;
  1281. }
  1282. copy_item_head(&dot_dot_ih,
  1283. tp_item_head(&dot_dot_entry_path));
  1284. /* node containing ".." gets into transaction */
  1285. reiserfs_prepare_for_journal(old_inode->i_sb,
  1286. dot_dot_de.de_bh, 1);
  1287. }
  1288. /*
  1289. * we should check seals here, not do
  1290. * this stuff, yes? Then, having
  1291. * gathered everything into RAM we
  1292. * should lock the buffers, yes? -Hans
  1293. */
  1294. /*
  1295. * probably. our rename needs to hold more
  1296. * than one path at once. The seals would
  1297. * have to be written to deal with multi-path
  1298. * issues -chris
  1299. */
  1300. /*
  1301. * sanity checking before doing the rename - avoid races many
  1302. * of the above checks could have scheduled. We have to be
  1303. * sure our items haven't been shifted by another process.
  1304. */
  1305. if (item_moved(&new_entry_ih, &new_entry_path) ||
  1306. !entry_points_to_object(new_dentry->d_name.name,
  1307. new_dentry->d_name.len,
  1308. &new_de, new_dentry_inode) ||
  1309. item_moved(&old_entry_ih, &old_entry_path) ||
  1310. !entry_points_to_object(old_dentry->d_name.name,
  1311. old_dentry->d_name.len,
  1312. &old_de, old_inode)) {
  1313. reiserfs_restore_prepared_buffer(old_inode->i_sb,
  1314. new_de.de_bh);
  1315. reiserfs_restore_prepared_buffer(old_inode->i_sb,
  1316. old_de.de_bh);
  1317. if (S_ISDIR(old_inode_mode))
  1318. reiserfs_restore_prepared_buffer(old_inode->
  1319. i_sb,
  1320. dot_dot_de.
  1321. de_bh);
  1322. continue;
  1323. }
  1324. if (S_ISDIR(old_inode_mode)) {
  1325. if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
  1326. !entry_points_to_object("..", 2, &dot_dot_de,
  1327. old_dir)) {
  1328. reiserfs_restore_prepared_buffer(old_inode->
  1329. i_sb,
  1330. old_de.de_bh);
  1331. reiserfs_restore_prepared_buffer(old_inode->
  1332. i_sb,
  1333. new_de.de_bh);
  1334. reiserfs_restore_prepared_buffer(old_inode->
  1335. i_sb,
  1336. dot_dot_de.
  1337. de_bh);
  1338. continue;
  1339. }
  1340. }
  1341. RFALSE(S_ISDIR(old_inode_mode) &&
  1342. !buffer_journal_prepared(dot_dot_de.de_bh), "");
  1343. break;
  1344. }
  1345. /*
  1346. * ok, all the changes can be done in one fell swoop when we
  1347. * have claimed all the buffers needed.
  1348. */
  1349. mark_de_visible(new_de.de_deh + new_de.de_entry_num);
  1350. set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
  1351. journal_mark_dirty(&th, new_de.de_bh);
  1352. mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
  1353. journal_mark_dirty(&th, old_de.de_bh);
  1354. ctime = current_time(old_dir);
  1355. old_dir->i_ctime = old_dir->i_mtime = ctime;
  1356. new_dir->i_ctime = new_dir->i_mtime = ctime;
  1357. /*
  1358. * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
  1359. * which adds ctime update of renamed object
  1360. */
  1361. old_inode->i_ctime = ctime;
  1362. if (new_dentry_inode) {
  1363. /* adjust link number of the victim */
  1364. if (S_ISDIR(new_dentry_inode->i_mode)) {
  1365. clear_nlink(new_dentry_inode);
  1366. } else {
  1367. drop_nlink(new_dentry_inode);
  1368. }
  1369. new_dentry_inode->i_ctime = ctime;
  1370. savelink = new_dentry_inode->i_nlink;
  1371. }
  1372. if (S_ISDIR(old_inode_mode)) {
  1373. /* adjust ".." of renamed directory */
  1374. set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
  1375. journal_mark_dirty(&th, dot_dot_de.de_bh);
  1376. /*
  1377. * there (in new_dir) was no directory, so it got new link
  1378. * (".." of renamed directory)
  1379. */
  1380. if (!new_dentry_inode)
  1381. INC_DIR_INODE_NLINK(new_dir);
  1382. /* old directory lost one link - ".. " of renamed directory */
  1383. DEC_DIR_INODE_NLINK(old_dir);
  1384. }
  1385. /*
  1386. * looks like in 2.3.99pre3 brelse is atomic.
  1387. * so we can use pathrelse
  1388. */
  1389. pathrelse(&new_entry_path);
  1390. pathrelse(&dot_dot_entry_path);
  1391. /*
  1392. * FIXME: this reiserfs_cut_from_item's return value may screw up
  1393. * anybody, but it will panic if will not be able to find the
  1394. * entry. This needs one more clean up
  1395. */
  1396. if (reiserfs_cut_from_item
  1397. (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
  1398. 0) < 0)
  1399. reiserfs_error(old_dir->i_sb, "vs-7060",
  1400. "couldn't not cut old name. Fsck later?");
  1401. old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
  1402. reiserfs_update_sd(&th, old_dir);
  1403. reiserfs_update_sd(&th, new_dir);
  1404. reiserfs_update_sd(&th, old_inode);
  1405. if (new_dentry_inode) {
  1406. if (savelink == 0)
  1407. add_save_link(&th, new_dentry_inode,
  1408. 0 /* not truncate */ );
  1409. reiserfs_update_sd(&th, new_dentry_inode);
  1410. }
  1411. retval = journal_end(&th);
  1412. reiserfs_write_unlock(old_dir->i_sb);
  1413. return retval;
  1414. }
  1415. /* directories can handle most operations... */
  1416. const struct inode_operations reiserfs_dir_inode_operations = {
  1417. .create = reiserfs_create,
  1418. .lookup = reiserfs_lookup,
  1419. .link = reiserfs_link,
  1420. .unlink = reiserfs_unlink,
  1421. .symlink = reiserfs_symlink,
  1422. .mkdir = reiserfs_mkdir,
  1423. .rmdir = reiserfs_rmdir,
  1424. .mknod = reiserfs_mknod,
  1425. .rename = reiserfs_rename,
  1426. .setattr = reiserfs_setattr,
  1427. .listxattr = reiserfs_listxattr,
  1428. .permission = reiserfs_permission,
  1429. .get_acl = reiserfs_get_acl,
  1430. .set_acl = reiserfs_set_acl,
  1431. };
  1432. /*
  1433. * symlink operations.. same as page_symlink_inode_operations, with xattr
  1434. * stuff added
  1435. */
  1436. const struct inode_operations reiserfs_symlink_inode_operations = {
  1437. .readlink = generic_readlink,
  1438. .get_link = page_get_link,
  1439. .setattr = reiserfs_setattr,
  1440. .listxattr = reiserfs_listxattr,
  1441. .permission = reiserfs_permission,
  1442. };
  1443. /*
  1444. * special file operations.. just xattr/acl stuff
  1445. */
  1446. const struct inode_operations reiserfs_special_inode_operations = {
  1447. .setattr = reiserfs_setattr,
  1448. .listxattr = reiserfs_listxattr,
  1449. .permission = reiserfs_permission,
  1450. .get_acl = reiserfs_get_acl,
  1451. .set_acl = reiserfs_set_acl,
  1452. };