namei.c 44 KB

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