namei.c 44 KB

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