namei.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /*
  2. * linux/fs/ext3/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd.h>
  29. #include <linux/time.h>
  30. #include <linux/ext3_fs.h>
  31. #include <linux/ext3_jbd.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/stat.h>
  34. #include <linux/string.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/bio.h>
  38. #include <linux/smp_lock.h>
  39. #include "namei.h"
  40. #include "xattr.h"
  41. #include "acl.h"
  42. /*
  43. * define how far ahead to read directories while searching them.
  44. */
  45. #define NAMEI_RA_CHUNKS 2
  46. #define NAMEI_RA_BLOCKS 4
  47. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  48. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  49. static struct buffer_head *ext3_append(handle_t *handle,
  50. struct inode *inode,
  51. u32 *block, int *err)
  52. {
  53. struct buffer_head *bh;
  54. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  55. if ((bh = ext3_bread(handle, inode, *block, 1, err))) {
  56. inode->i_size += inode->i_sb->s_blocksize;
  57. EXT3_I(inode)->i_disksize = inode->i_size;
  58. ext3_journal_get_write_access(handle,bh);
  59. }
  60. return bh;
  61. }
  62. #ifndef assert
  63. #define assert(test) J_ASSERT(test)
  64. #endif
  65. #ifndef swap
  66. #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
  67. #endif
  68. #ifdef DX_DEBUG
  69. #define dxtrace(command) command
  70. #else
  71. #define dxtrace(command)
  72. #endif
  73. struct fake_dirent
  74. {
  75. __le32 inode;
  76. __le16 rec_len;
  77. u8 name_len;
  78. u8 file_type;
  79. };
  80. struct dx_countlimit
  81. {
  82. __le16 limit;
  83. __le16 count;
  84. };
  85. struct dx_entry
  86. {
  87. __le32 hash;
  88. __le32 block;
  89. };
  90. /*
  91. * dx_root_info is laid out so that if it should somehow get overlaid by a
  92. * dirent the two low bits of the hash version will be zero. Therefore, the
  93. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  94. */
  95. struct dx_root
  96. {
  97. struct fake_dirent dot;
  98. char dot_name[4];
  99. struct fake_dirent dotdot;
  100. char dotdot_name[4];
  101. struct dx_root_info
  102. {
  103. __le32 reserved_zero;
  104. u8 hash_version;
  105. u8 info_length; /* 8 */
  106. u8 indirect_levels;
  107. u8 unused_flags;
  108. }
  109. info;
  110. struct dx_entry entries[0];
  111. };
  112. struct dx_node
  113. {
  114. struct fake_dirent fake;
  115. struct dx_entry entries[0];
  116. };
  117. struct dx_frame
  118. {
  119. struct buffer_head *bh;
  120. struct dx_entry *entries;
  121. struct dx_entry *at;
  122. };
  123. struct dx_map_entry
  124. {
  125. u32 hash;
  126. u32 offs;
  127. };
  128. #ifdef CONFIG_EXT3_INDEX
  129. static inline unsigned dx_get_block (struct dx_entry *entry);
  130. static void dx_set_block (struct dx_entry *entry, unsigned value);
  131. static inline unsigned dx_get_hash (struct dx_entry *entry);
  132. static void dx_set_hash (struct dx_entry *entry, unsigned value);
  133. static unsigned dx_get_count (struct dx_entry *entries);
  134. static unsigned dx_get_limit (struct dx_entry *entries);
  135. static void dx_set_count (struct dx_entry *entries, unsigned value);
  136. static void dx_set_limit (struct dx_entry *entries, unsigned value);
  137. static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
  138. static unsigned dx_node_limit (struct inode *dir);
  139. static struct dx_frame *dx_probe(struct dentry *dentry,
  140. struct inode *dir,
  141. struct dx_hash_info *hinfo,
  142. struct dx_frame *frame,
  143. int *err);
  144. static void dx_release (struct dx_frame *frames);
  145. static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
  146. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  147. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  148. static struct ext3_dir_entry_2 *dx_move_dirents (char *from, char *to,
  149. struct dx_map_entry *offsets, int count);
  150. static struct ext3_dir_entry_2* dx_pack_dirents (char *base, int size);
  151. static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
  152. static int ext3_htree_next_block(struct inode *dir, __u32 hash,
  153. struct dx_frame *frame,
  154. struct dx_frame *frames,
  155. __u32 *start_hash);
  156. static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
  157. struct ext3_dir_entry_2 **res_dir, int *err);
  158. static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
  159. struct inode *inode);
  160. /*
  161. * Future: use high four bits of block for coalesce-on-delete flags
  162. * Mask them off for now.
  163. */
  164. static inline unsigned dx_get_block (struct dx_entry *entry)
  165. {
  166. return le32_to_cpu(entry->block) & 0x00ffffff;
  167. }
  168. static inline void dx_set_block (struct dx_entry *entry, unsigned value)
  169. {
  170. entry->block = cpu_to_le32(value);
  171. }
  172. static inline unsigned dx_get_hash (struct dx_entry *entry)
  173. {
  174. return le32_to_cpu(entry->hash);
  175. }
  176. static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
  177. {
  178. entry->hash = cpu_to_le32(value);
  179. }
  180. static inline unsigned dx_get_count (struct dx_entry *entries)
  181. {
  182. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  183. }
  184. static inline unsigned dx_get_limit (struct dx_entry *entries)
  185. {
  186. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  187. }
  188. static inline void dx_set_count (struct dx_entry *entries, unsigned value)
  189. {
  190. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  191. }
  192. static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
  193. {
  194. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  195. }
  196. static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
  197. {
  198. unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(1) -
  199. EXT3_DIR_REC_LEN(2) - infosize;
  200. return 0? 20: entry_space / sizeof(struct dx_entry);
  201. }
  202. static inline unsigned dx_node_limit (struct inode *dir)
  203. {
  204. unsigned entry_space = dir->i_sb->s_blocksize - EXT3_DIR_REC_LEN(0);
  205. return 0? 22: entry_space / sizeof(struct dx_entry);
  206. }
  207. /*
  208. * Debug
  209. */
  210. #ifdef DX_DEBUG
  211. static void dx_show_index (char * label, struct dx_entry *entries)
  212. {
  213. int i, n = dx_get_count (entries);
  214. printk("%s index ", label);
  215. for (i = 0; i < n; i++)
  216. {
  217. printk("%x->%u ", i? dx_get_hash(entries + i): 0, dx_get_block(entries + i));
  218. }
  219. printk("\n");
  220. }
  221. struct stats
  222. {
  223. unsigned names;
  224. unsigned space;
  225. unsigned bcount;
  226. };
  227. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext3_dir_entry_2 *de,
  228. int size, int show_names)
  229. {
  230. unsigned names = 0, space = 0;
  231. char *base = (char *) de;
  232. struct dx_hash_info h = *hinfo;
  233. printk("names: ");
  234. while ((char *) de < base + size)
  235. {
  236. if (de->inode)
  237. {
  238. if (show_names)
  239. {
  240. int len = de->name_len;
  241. char *name = de->name;
  242. while (len--) printk("%c", *name++);
  243. ext3fs_dirhash(de->name, de->name_len, &h);
  244. printk(":%x.%u ", h.hash,
  245. ((char *) de - base));
  246. }
  247. space += EXT3_DIR_REC_LEN(de->name_len);
  248. names++;
  249. }
  250. de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
  251. }
  252. printk("(%i)\n", names);
  253. return (struct stats) { names, space, 1 };
  254. }
  255. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  256. struct dx_entry *entries, int levels)
  257. {
  258. unsigned blocksize = dir->i_sb->s_blocksize;
  259. unsigned count = dx_get_count (entries), names = 0, space = 0, i;
  260. unsigned bcount = 0;
  261. struct buffer_head *bh;
  262. int err;
  263. printk("%i indexed blocks...\n", count);
  264. for (i = 0; i < count; i++, entries++)
  265. {
  266. u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
  267. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  268. struct stats stats;
  269. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  270. if (!(bh = ext3_bread (NULL,dir, block, 0,&err))) continue;
  271. stats = levels?
  272. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  273. dx_show_leaf(hinfo, (struct ext3_dir_entry_2 *) bh->b_data, blocksize, 0);
  274. names += stats.names;
  275. space += stats.space;
  276. bcount += stats.bcount;
  277. brelse (bh);
  278. }
  279. if (bcount)
  280. printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
  281. names, space/bcount,(space/bcount)*100/blocksize);
  282. return (struct stats) { names, space, bcount};
  283. }
  284. #endif /* DX_DEBUG */
  285. /*
  286. * Probe for a directory leaf block to search.
  287. *
  288. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  289. * error in the directory index, and the caller should fall back to
  290. * searching the directory normally. The callers of dx_probe **MUST**
  291. * check for this error code, and make sure it never gets reflected
  292. * back to userspace.
  293. */
  294. static struct dx_frame *
  295. dx_probe(struct dentry *dentry, struct inode *dir,
  296. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  297. {
  298. unsigned count, indirect;
  299. struct dx_entry *at, *entries, *p, *q, *m;
  300. struct dx_root *root;
  301. struct buffer_head *bh;
  302. struct dx_frame *frame = frame_in;
  303. u32 hash;
  304. frame->bh = NULL;
  305. if (dentry)
  306. dir = dentry->d_parent->d_inode;
  307. if (!(bh = ext3_bread (NULL,dir, 0, 0, err)))
  308. goto fail;
  309. root = (struct dx_root *) bh->b_data;
  310. if (root->info.hash_version != DX_HASH_TEA &&
  311. root->info.hash_version != DX_HASH_HALF_MD4 &&
  312. root->info.hash_version != DX_HASH_LEGACY) {
  313. ext3_warning(dir->i_sb, __FUNCTION__,
  314. "Unrecognised inode hash code %d",
  315. root->info.hash_version);
  316. brelse(bh);
  317. *err = ERR_BAD_DX_DIR;
  318. goto fail;
  319. }
  320. hinfo->hash_version = root->info.hash_version;
  321. hinfo->seed = EXT3_SB(dir->i_sb)->s_hash_seed;
  322. if (dentry)
  323. ext3fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
  324. hash = hinfo->hash;
  325. if (root->info.unused_flags & 1) {
  326. ext3_warning(dir->i_sb, __FUNCTION__,
  327. "Unimplemented inode hash flags: %#06x",
  328. root->info.unused_flags);
  329. brelse(bh);
  330. *err = ERR_BAD_DX_DIR;
  331. goto fail;
  332. }
  333. if ((indirect = root->info.indirect_levels) > 1) {
  334. ext3_warning(dir->i_sb, __FUNCTION__,
  335. "Unimplemented inode hash depth: %#06x",
  336. root->info.indirect_levels);
  337. brelse(bh);
  338. *err = ERR_BAD_DX_DIR;
  339. goto fail;
  340. }
  341. entries = (struct dx_entry *) (((char *)&root->info) +
  342. root->info.info_length);
  343. assert(dx_get_limit(entries) == dx_root_limit(dir,
  344. root->info.info_length));
  345. dxtrace (printk("Look up %x", hash));
  346. while (1)
  347. {
  348. count = dx_get_count(entries);
  349. assert (count && count <= dx_get_limit(entries));
  350. p = entries + 1;
  351. q = entries + count - 1;
  352. while (p <= q)
  353. {
  354. m = p + (q - p)/2;
  355. dxtrace(printk("."));
  356. if (dx_get_hash(m) > hash)
  357. q = m - 1;
  358. else
  359. p = m + 1;
  360. }
  361. if (0) // linear search cross check
  362. {
  363. unsigned n = count - 1;
  364. at = entries;
  365. while (n--)
  366. {
  367. dxtrace(printk(","));
  368. if (dx_get_hash(++at) > hash)
  369. {
  370. at--;
  371. break;
  372. }
  373. }
  374. assert (at == p - 1);
  375. }
  376. at = p - 1;
  377. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  378. frame->bh = bh;
  379. frame->entries = entries;
  380. frame->at = at;
  381. if (!indirect--) return frame;
  382. if (!(bh = ext3_bread (NULL,dir, dx_get_block(at), 0, err)))
  383. goto fail2;
  384. at = entries = ((struct dx_node *) bh->b_data)->entries;
  385. assert (dx_get_limit(entries) == dx_node_limit (dir));
  386. frame++;
  387. }
  388. fail2:
  389. while (frame >= frame_in) {
  390. brelse(frame->bh);
  391. frame--;
  392. }
  393. fail:
  394. return NULL;
  395. }
  396. static void dx_release (struct dx_frame *frames)
  397. {
  398. if (frames[0].bh == NULL)
  399. return;
  400. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  401. brelse(frames[1].bh);
  402. brelse(frames[0].bh);
  403. }
  404. /*
  405. * This function increments the frame pointer to search the next leaf
  406. * block, and reads in the necessary intervening nodes if the search
  407. * should be necessary. Whether or not the search is necessary is
  408. * controlled by the hash parameter. If the hash value is even, then
  409. * the search is only continued if the next block starts with that
  410. * hash value. This is used if we are searching for a specific file.
  411. *
  412. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  413. *
  414. * This function returns 1 if the caller should continue to search,
  415. * or 0 if it should not. If there is an error reading one of the
  416. * index blocks, it will a negative error code.
  417. *
  418. * If start_hash is non-null, it will be filled in with the starting
  419. * hash of the next page.
  420. */
  421. static int ext3_htree_next_block(struct inode *dir, __u32 hash,
  422. struct dx_frame *frame,
  423. struct dx_frame *frames,
  424. __u32 *start_hash)
  425. {
  426. struct dx_frame *p;
  427. struct buffer_head *bh;
  428. int err, num_frames = 0;
  429. __u32 bhash;
  430. p = frame;
  431. /*
  432. * Find the next leaf page by incrementing the frame pointer.
  433. * If we run out of entries in the interior node, loop around and
  434. * increment pointer in the parent node. When we break out of
  435. * this loop, num_frames indicates the number of interior
  436. * nodes need to be read.
  437. */
  438. while (1) {
  439. if (++(p->at) < p->entries + dx_get_count(p->entries))
  440. break;
  441. if (p == frames)
  442. return 0;
  443. num_frames++;
  444. p--;
  445. }
  446. /*
  447. * If the hash is 1, then continue only if the next page has a
  448. * continuation hash of any value. This is used for readdir
  449. * handling. Otherwise, check to see if the hash matches the
  450. * desired contiuation hash. If it doesn't, return since
  451. * there's no point to read in the successive index pages.
  452. */
  453. bhash = dx_get_hash(p->at);
  454. if (start_hash)
  455. *start_hash = bhash;
  456. if ((hash & 1) == 0) {
  457. if ((bhash & ~1) != hash)
  458. return 0;
  459. }
  460. /*
  461. * If the hash is HASH_NB_ALWAYS, we always go to the next
  462. * block so no check is necessary
  463. */
  464. while (num_frames--) {
  465. if (!(bh = ext3_bread(NULL, dir, dx_get_block(p->at),
  466. 0, &err)))
  467. return err; /* Failure */
  468. p++;
  469. brelse (p->bh);
  470. p->bh = bh;
  471. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  472. }
  473. return 1;
  474. }
  475. /*
  476. * p is at least 6 bytes before the end of page
  477. */
  478. static inline struct ext3_dir_entry_2 *ext3_next_entry(struct ext3_dir_entry_2 *p)
  479. {
  480. return (struct ext3_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
  481. }
  482. /*
  483. * This function fills a red-black tree with information from a
  484. * directory block. It returns the number directory entries loaded
  485. * into the tree. If there is an error it is returned in err.
  486. */
  487. static int htree_dirblock_to_tree(struct file *dir_file,
  488. struct inode *dir, int block,
  489. struct dx_hash_info *hinfo,
  490. __u32 start_hash, __u32 start_minor_hash)
  491. {
  492. struct buffer_head *bh;
  493. struct ext3_dir_entry_2 *de, *top;
  494. int err, count = 0;
  495. dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
  496. if (!(bh = ext3_bread (NULL, dir, block, 0, &err)))
  497. return err;
  498. de = (struct ext3_dir_entry_2 *) bh->b_data;
  499. top = (struct ext3_dir_entry_2 *) ((char *) de +
  500. dir->i_sb->s_blocksize -
  501. EXT3_DIR_REC_LEN(0));
  502. for (; de < top; de = ext3_next_entry(de)) {
  503. if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  504. (block<<EXT3_BLOCK_SIZE_BITS(dir->i_sb))
  505. +((char *)de - bh->b_data))) {
  506. /* On error, skip the f_pos to the next block. */
  507. dir_file->f_pos = (dir_file->f_pos |
  508. (dir->i_sb->s_blocksize - 1)) + 1;
  509. brelse (bh);
  510. return count;
  511. }
  512. ext3fs_dirhash(de->name, de->name_len, hinfo);
  513. if ((hinfo->hash < start_hash) ||
  514. ((hinfo->hash == start_hash) &&
  515. (hinfo->minor_hash < start_minor_hash)))
  516. continue;
  517. if (de->inode == 0)
  518. continue;
  519. if ((err = ext3_htree_store_dirent(dir_file,
  520. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  521. brelse(bh);
  522. return err;
  523. }
  524. count++;
  525. }
  526. brelse(bh);
  527. return count;
  528. }
  529. /*
  530. * This function fills a red-black tree with information from a
  531. * directory. We start scanning the directory in hash order, starting
  532. * at start_hash and start_minor_hash.
  533. *
  534. * This function returns the number of entries inserted into the tree,
  535. * or a negative error code.
  536. */
  537. int ext3_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  538. __u32 start_minor_hash, __u32 *next_hash)
  539. {
  540. struct dx_hash_info hinfo;
  541. struct ext3_dir_entry_2 *de;
  542. struct dx_frame frames[2], *frame;
  543. struct inode *dir;
  544. int block, err;
  545. int count = 0;
  546. int ret;
  547. __u32 hashval;
  548. dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
  549. start_minor_hash));
  550. dir = dir_file->f_path.dentry->d_inode;
  551. if (!(EXT3_I(dir)->i_flags & EXT3_INDEX_FL)) {
  552. hinfo.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
  553. hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
  554. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  555. start_hash, start_minor_hash);
  556. *next_hash = ~0;
  557. return count;
  558. }
  559. hinfo.hash = start_hash;
  560. hinfo.minor_hash = 0;
  561. frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
  562. if (!frame)
  563. return err;
  564. /* Add '.' and '..' from the htree header */
  565. if (!start_hash && !start_minor_hash) {
  566. de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
  567. if ((err = ext3_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  568. goto errout;
  569. count++;
  570. }
  571. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  572. de = (struct ext3_dir_entry_2 *) frames[0].bh->b_data;
  573. de = ext3_next_entry(de);
  574. if ((err = ext3_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  575. goto errout;
  576. count++;
  577. }
  578. while (1) {
  579. block = dx_get_block(frame->at);
  580. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  581. start_hash, start_minor_hash);
  582. if (ret < 0) {
  583. err = ret;
  584. goto errout;
  585. }
  586. count += ret;
  587. hashval = ~0;
  588. ret = ext3_htree_next_block(dir, HASH_NB_ALWAYS,
  589. frame, frames, &hashval);
  590. *next_hash = hashval;
  591. if (ret < 0) {
  592. err = ret;
  593. goto errout;
  594. }
  595. /*
  596. * Stop if: (a) there are no more entries, or
  597. * (b) we have inserted at least one entry and the
  598. * next hash value is not a continuation
  599. */
  600. if ((ret == 0) ||
  601. (count && ((hashval & 1) == 0)))
  602. break;
  603. }
  604. dx_release(frames);
  605. dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
  606. count, *next_hash));
  607. return count;
  608. errout:
  609. dx_release(frames);
  610. return (err);
  611. }
  612. /*
  613. * Directory block splitting, compacting
  614. */
  615. static int dx_make_map (struct ext3_dir_entry_2 *de, int size,
  616. struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
  617. {
  618. int count = 0;
  619. char *base = (char *) de;
  620. struct dx_hash_info h = *hinfo;
  621. while ((char *) de < base + size)
  622. {
  623. if (de->name_len && de->inode) {
  624. ext3fs_dirhash(de->name, de->name_len, &h);
  625. map_tail--;
  626. map_tail->hash = h.hash;
  627. map_tail->offs = (u32) ((char *) de - base);
  628. count++;
  629. cond_resched();
  630. }
  631. /* XXX: do we need to check rec_len == 0 case? -Chris */
  632. de = (struct ext3_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
  633. }
  634. return count;
  635. }
  636. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  637. {
  638. struct dx_map_entry *p, *q, *top = map + count - 1;
  639. int more;
  640. /* Combsort until bubble sort doesn't suck */
  641. while (count > 2)
  642. {
  643. count = count*10/13;
  644. if (count - 9 < 2) /* 9, 10 -> 11 */
  645. count = 11;
  646. for (p = top, q = p - count; q >= map; p--, q--)
  647. if (p->hash < q->hash)
  648. swap(*p, *q);
  649. }
  650. /* Garden variety bubble sort */
  651. do {
  652. more = 0;
  653. q = top;
  654. while (q-- > map)
  655. {
  656. if (q[1].hash >= q[0].hash)
  657. continue;
  658. swap(*(q+1), *q);
  659. more = 1;
  660. }
  661. } while(more);
  662. }
  663. static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
  664. {
  665. struct dx_entry *entries = frame->entries;
  666. struct dx_entry *old = frame->at, *new = old + 1;
  667. int count = dx_get_count(entries);
  668. assert(count < dx_get_limit(entries));
  669. assert(old < entries + count);
  670. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  671. dx_set_hash(new, hash);
  672. dx_set_block(new, block);
  673. dx_set_count(entries, count + 1);
  674. }
  675. #endif
  676. static void ext3_update_dx_flag(struct inode *inode)
  677. {
  678. if (!EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
  679. EXT3_FEATURE_COMPAT_DIR_INDEX))
  680. EXT3_I(inode)->i_flags &= ~EXT3_INDEX_FL;
  681. }
  682. /*
  683. * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure.
  684. *
  685. * `len <= EXT3_NAME_LEN' is guaranteed by caller.
  686. * `de != NULL' is guaranteed by caller.
  687. */
  688. static inline int ext3_match (int len, const char * const name,
  689. struct ext3_dir_entry_2 * de)
  690. {
  691. if (len != de->name_len)
  692. return 0;
  693. if (!de->inode)
  694. return 0;
  695. return !memcmp(name, de->name, len);
  696. }
  697. /*
  698. * Returns 0 if not found, -1 on failure, and 1 on success
  699. */
  700. static inline int search_dirblock(struct buffer_head * bh,
  701. struct inode *dir,
  702. struct dentry *dentry,
  703. unsigned long offset,
  704. struct ext3_dir_entry_2 ** res_dir)
  705. {
  706. struct ext3_dir_entry_2 * de;
  707. char * dlimit;
  708. int de_len;
  709. const char *name = dentry->d_name.name;
  710. int namelen = dentry->d_name.len;
  711. de = (struct ext3_dir_entry_2 *) bh->b_data;
  712. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  713. while ((char *) de < dlimit) {
  714. /* this code is executed quadratically often */
  715. /* do minimal checking `by hand' */
  716. if ((char *) de + namelen <= dlimit &&
  717. ext3_match (namelen, name, de)) {
  718. /* found a match - just to be sure, do a full check */
  719. if (!ext3_check_dir_entry("ext3_find_entry",
  720. dir, de, bh, offset))
  721. return -1;
  722. *res_dir = de;
  723. return 1;
  724. }
  725. /* prevent looping on a bad block */
  726. de_len = le16_to_cpu(de->rec_len);
  727. if (de_len <= 0)
  728. return -1;
  729. offset += de_len;
  730. de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);
  731. }
  732. return 0;
  733. }
  734. /*
  735. * ext3_find_entry()
  736. *
  737. * finds an entry in the specified directory with the wanted name. It
  738. * returns the cache buffer in which the entry was found, and the entry
  739. * itself (as a parameter - res_dir). It does NOT read the inode of the
  740. * entry - you'll have to do that yourself if you want to.
  741. *
  742. * The returned buffer_head has ->b_count elevated. The caller is expected
  743. * to brelse() it when appropriate.
  744. */
  745. static struct buffer_head * ext3_find_entry (struct dentry *dentry,
  746. struct ext3_dir_entry_2 ** res_dir)
  747. {
  748. struct super_block * sb;
  749. struct buffer_head * bh_use[NAMEI_RA_SIZE];
  750. struct buffer_head * bh, *ret = NULL;
  751. unsigned long start, block, b;
  752. int ra_max = 0; /* Number of bh's in the readahead
  753. buffer, bh_use[] */
  754. int ra_ptr = 0; /* Current index into readahead
  755. buffer */
  756. int num = 0;
  757. int nblocks, i, err;
  758. struct inode *dir = dentry->d_parent->d_inode;
  759. int namelen;
  760. const u8 *name;
  761. unsigned blocksize;
  762. *res_dir = NULL;
  763. sb = dir->i_sb;
  764. blocksize = sb->s_blocksize;
  765. namelen = dentry->d_name.len;
  766. name = dentry->d_name.name;
  767. if (namelen > EXT3_NAME_LEN)
  768. return NULL;
  769. #ifdef CONFIG_EXT3_INDEX
  770. if (is_dx(dir)) {
  771. bh = ext3_dx_find_entry(dentry, res_dir, &err);
  772. /*
  773. * On success, or if the error was file not found,
  774. * return. Otherwise, fall back to doing a search the
  775. * old fashioned way.
  776. */
  777. if (bh || (err != ERR_BAD_DX_DIR))
  778. return bh;
  779. dxtrace(printk("ext3_find_entry: dx failed, falling back\n"));
  780. }
  781. #endif
  782. nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
  783. start = EXT3_I(dir)->i_dir_start_lookup;
  784. if (start >= nblocks)
  785. start = 0;
  786. block = start;
  787. restart:
  788. do {
  789. /*
  790. * We deal with the read-ahead logic here.
  791. */
  792. if (ra_ptr >= ra_max) {
  793. /* Refill the readahead buffer */
  794. ra_ptr = 0;
  795. b = block;
  796. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  797. /*
  798. * Terminate if we reach the end of the
  799. * directory and must wrap, or if our
  800. * search has finished at this block.
  801. */
  802. if (b >= nblocks || (num && block == start)) {
  803. bh_use[ra_max] = NULL;
  804. break;
  805. }
  806. num++;
  807. bh = ext3_getblk(NULL, dir, b++, 0, &err);
  808. bh_use[ra_max] = bh;
  809. if (bh)
  810. ll_rw_block(READ_META, 1, &bh);
  811. }
  812. }
  813. if ((bh = bh_use[ra_ptr++]) == NULL)
  814. goto next;
  815. wait_on_buffer(bh);
  816. if (!buffer_uptodate(bh)) {
  817. /* read error, skip block & hope for the best */
  818. ext3_error(sb, __FUNCTION__, "reading directory #%lu "
  819. "offset %lu", dir->i_ino, block);
  820. brelse(bh);
  821. goto next;
  822. }
  823. i = search_dirblock(bh, dir, dentry,
  824. block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);
  825. if (i == 1) {
  826. EXT3_I(dir)->i_dir_start_lookup = block;
  827. ret = bh;
  828. goto cleanup_and_exit;
  829. } else {
  830. brelse(bh);
  831. if (i < 0)
  832. goto cleanup_and_exit;
  833. }
  834. next:
  835. if (++block >= nblocks)
  836. block = 0;
  837. } while (block != start);
  838. /*
  839. * If the directory has grown while we were searching, then
  840. * search the last part of the directory before giving up.
  841. */
  842. block = nblocks;
  843. nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);
  844. if (block < nblocks) {
  845. start = 0;
  846. goto restart;
  847. }
  848. cleanup_and_exit:
  849. /* Clean up the read-ahead blocks */
  850. for (; ra_ptr < ra_max; ra_ptr++)
  851. brelse (bh_use[ra_ptr]);
  852. return ret;
  853. }
  854. #ifdef CONFIG_EXT3_INDEX
  855. static struct buffer_head * ext3_dx_find_entry(struct dentry *dentry,
  856. struct ext3_dir_entry_2 **res_dir, int *err)
  857. {
  858. struct super_block * sb;
  859. struct dx_hash_info hinfo;
  860. u32 hash;
  861. struct dx_frame frames[2], *frame;
  862. struct ext3_dir_entry_2 *de, *top;
  863. struct buffer_head *bh;
  864. unsigned long block;
  865. int retval;
  866. int namelen = dentry->d_name.len;
  867. const u8 *name = dentry->d_name.name;
  868. struct inode *dir = dentry->d_parent->d_inode;
  869. sb = dir->i_sb;
  870. /* NFS may look up ".." - look at dx_root directory block */
  871. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  872. if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
  873. return NULL;
  874. } else {
  875. frame = frames;
  876. frame->bh = NULL; /* for dx_release() */
  877. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  878. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  879. }
  880. hash = hinfo.hash;
  881. do {
  882. block = dx_get_block(frame->at);
  883. if (!(bh = ext3_bread (NULL,dir, block, 0, err)))
  884. goto errout;
  885. de = (struct ext3_dir_entry_2 *) bh->b_data;
  886. top = (struct ext3_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  887. EXT3_DIR_REC_LEN(0));
  888. for (; de < top; de = ext3_next_entry(de))
  889. if (ext3_match (namelen, name, de)) {
  890. if (!ext3_check_dir_entry("ext3_find_entry",
  891. dir, de, bh,
  892. (block<<EXT3_BLOCK_SIZE_BITS(sb))
  893. +((char *)de - bh->b_data))) {
  894. brelse (bh);
  895. goto errout;
  896. }
  897. *res_dir = de;
  898. dx_release (frames);
  899. return bh;
  900. }
  901. brelse (bh);
  902. /* Check to see if we should continue to search */
  903. retval = ext3_htree_next_block(dir, hash, frame,
  904. frames, NULL);
  905. if (retval < 0) {
  906. ext3_warning(sb, __FUNCTION__,
  907. "error reading index page in directory #%lu",
  908. dir->i_ino);
  909. *err = retval;
  910. goto errout;
  911. }
  912. } while (retval == 1);
  913. *err = -ENOENT;
  914. errout:
  915. dxtrace(printk("%s not found\n", name));
  916. dx_release (frames);
  917. return NULL;
  918. }
  919. #endif
  920. static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  921. {
  922. struct inode * inode;
  923. struct ext3_dir_entry_2 * de;
  924. struct buffer_head * bh;
  925. if (dentry->d_name.len > EXT3_NAME_LEN)
  926. return ERR_PTR(-ENAMETOOLONG);
  927. bh = ext3_find_entry(dentry, &de);
  928. inode = NULL;
  929. if (bh) {
  930. unsigned long ino = le32_to_cpu(de->inode);
  931. brelse (bh);
  932. if (!ext3_valid_inum(dir->i_sb, ino)) {
  933. ext3_error(dir->i_sb, "ext3_lookup",
  934. "bad inode number: %lu", ino);
  935. inode = NULL;
  936. } else
  937. inode = iget(dir->i_sb, ino);
  938. if (!inode)
  939. return ERR_PTR(-EACCES);
  940. }
  941. return d_splice_alias(inode, dentry);
  942. }
  943. struct dentry *ext3_get_parent(struct dentry *child)
  944. {
  945. unsigned long ino;
  946. struct dentry *parent;
  947. struct inode *inode;
  948. struct dentry dotdot;
  949. struct ext3_dir_entry_2 * de;
  950. struct buffer_head *bh;
  951. dotdot.d_name.name = "..";
  952. dotdot.d_name.len = 2;
  953. dotdot.d_parent = child; /* confusing, isn't it! */
  954. bh = ext3_find_entry(&dotdot, &de);
  955. inode = NULL;
  956. if (!bh)
  957. return ERR_PTR(-ENOENT);
  958. ino = le32_to_cpu(de->inode);
  959. brelse(bh);
  960. if (!ext3_valid_inum(child->d_inode->i_sb, ino)) {
  961. ext3_error(child->d_inode->i_sb, "ext3_get_parent",
  962. "bad inode number: %lu", ino);
  963. inode = NULL;
  964. } else
  965. inode = iget(child->d_inode->i_sb, ino);
  966. if (!inode)
  967. return ERR_PTR(-EACCES);
  968. parent = d_alloc_anon(inode);
  969. if (!parent) {
  970. iput(inode);
  971. parent = ERR_PTR(-ENOMEM);
  972. }
  973. return parent;
  974. }
  975. #define S_SHIFT 12
  976. static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {
  977. [S_IFREG >> S_SHIFT] = EXT3_FT_REG_FILE,
  978. [S_IFDIR >> S_SHIFT] = EXT3_FT_DIR,
  979. [S_IFCHR >> S_SHIFT] = EXT3_FT_CHRDEV,
  980. [S_IFBLK >> S_SHIFT] = EXT3_FT_BLKDEV,
  981. [S_IFIFO >> S_SHIFT] = EXT3_FT_FIFO,
  982. [S_IFSOCK >> S_SHIFT] = EXT3_FT_SOCK,
  983. [S_IFLNK >> S_SHIFT] = EXT3_FT_SYMLINK,
  984. };
  985. static inline void ext3_set_de_type(struct super_block *sb,
  986. struct ext3_dir_entry_2 *de,
  987. umode_t mode) {
  988. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))
  989. de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  990. }
  991. #ifdef CONFIG_EXT3_INDEX
  992. static struct ext3_dir_entry_2 *
  993. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
  994. {
  995. unsigned rec_len = 0;
  996. while (count--) {
  997. struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *) (from + map->offs);
  998. rec_len = EXT3_DIR_REC_LEN(de->name_len);
  999. memcpy (to, de, rec_len);
  1000. ((struct ext3_dir_entry_2 *) to)->rec_len =
  1001. cpu_to_le16(rec_len);
  1002. de->inode = 0;
  1003. map++;
  1004. to += rec_len;
  1005. }
  1006. return (struct ext3_dir_entry_2 *) (to - rec_len);
  1007. }
  1008. static struct ext3_dir_entry_2* dx_pack_dirents(char *base, int size)
  1009. {
  1010. struct ext3_dir_entry_2 *next, *to, *prev, *de = (struct ext3_dir_entry_2 *) base;
  1011. unsigned rec_len = 0;
  1012. prev = to = de;
  1013. while ((char*)de < base + size) {
  1014. next = (struct ext3_dir_entry_2 *) ((char *) de +
  1015. le16_to_cpu(de->rec_len));
  1016. if (de->inode && de->name_len) {
  1017. rec_len = EXT3_DIR_REC_LEN(de->name_len);
  1018. if (de > to)
  1019. memmove(to, de, rec_len);
  1020. to->rec_len = cpu_to_le16(rec_len);
  1021. prev = to;
  1022. to = (struct ext3_dir_entry_2 *) (((char *) to) + rec_len);
  1023. }
  1024. de = next;
  1025. }
  1026. return prev;
  1027. }
  1028. static struct ext3_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1029. struct buffer_head **bh,struct dx_frame *frame,
  1030. struct dx_hash_info *hinfo, int *error)
  1031. {
  1032. unsigned blocksize = dir->i_sb->s_blocksize;
  1033. unsigned count, continued;
  1034. struct buffer_head *bh2;
  1035. u32 newblock;
  1036. u32 hash2;
  1037. struct dx_map_entry *map;
  1038. char *data1 = (*bh)->b_data, *data2;
  1039. unsigned split;
  1040. struct ext3_dir_entry_2 *de = NULL, *de2;
  1041. int err;
  1042. bh2 = ext3_append (handle, dir, &newblock, error);
  1043. if (!(bh2)) {
  1044. brelse(*bh);
  1045. *bh = NULL;
  1046. goto errout;
  1047. }
  1048. BUFFER_TRACE(*bh, "get_write_access");
  1049. err = ext3_journal_get_write_access(handle, *bh);
  1050. if (err) {
  1051. journal_error:
  1052. brelse(*bh);
  1053. brelse(bh2);
  1054. *bh = NULL;
  1055. ext3_std_error(dir->i_sb, err);
  1056. goto errout;
  1057. }
  1058. BUFFER_TRACE(frame->bh, "get_write_access");
  1059. err = ext3_journal_get_write_access(handle, frame->bh);
  1060. if (err)
  1061. goto journal_error;
  1062. data2 = bh2->b_data;
  1063. /* create map in the end of data2 block */
  1064. map = (struct dx_map_entry *) (data2 + blocksize);
  1065. count = dx_make_map ((struct ext3_dir_entry_2 *) data1,
  1066. blocksize, hinfo, map);
  1067. map -= count;
  1068. split = count/2; // need to adjust to actual middle
  1069. dx_sort_map (map, count);
  1070. hash2 = map[split].hash;
  1071. continued = hash2 == map[split - 1].hash;
  1072. dxtrace(printk("Split block %i at %x, %i/%i\n",
  1073. dx_get_block(frame->at), hash2, split, count-split));
  1074. /* Fancy dance to stay within two buffers */
  1075. de2 = dx_move_dirents(data1, data2, map + split, count - split);
  1076. de = dx_pack_dirents(data1,blocksize);
  1077. de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
  1078. de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
  1079. dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data1, blocksize, 1));
  1080. dxtrace(dx_show_leaf (hinfo, (struct ext3_dir_entry_2 *) data2, blocksize, 1));
  1081. /* Which block gets the new entry? */
  1082. if (hinfo->hash >= hash2)
  1083. {
  1084. swap(*bh, bh2);
  1085. de = de2;
  1086. }
  1087. dx_insert_block (frame, hash2 + continued, newblock);
  1088. err = ext3_journal_dirty_metadata (handle, bh2);
  1089. if (err)
  1090. goto journal_error;
  1091. err = ext3_journal_dirty_metadata (handle, frame->bh);
  1092. if (err)
  1093. goto journal_error;
  1094. brelse (bh2);
  1095. dxtrace(dx_show_index ("frame", frame->entries));
  1096. errout:
  1097. return de;
  1098. }
  1099. #endif
  1100. /*
  1101. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1102. * it points to a directory entry which is guaranteed to be large
  1103. * enough for new directory entry. If de is NULL, then
  1104. * add_dirent_to_buf will attempt search the directory block for
  1105. * space. It will return -ENOSPC if no space is available, and -EIO
  1106. * and -EEXIST if directory entry already exists.
  1107. *
  1108. * NOTE! bh is NOT released in the case where ENOSPC is returned. In
  1109. * all other cases bh is released.
  1110. */
  1111. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1112. struct inode *inode, struct ext3_dir_entry_2 *de,
  1113. struct buffer_head * bh)
  1114. {
  1115. struct inode *dir = dentry->d_parent->d_inode;
  1116. const char *name = dentry->d_name.name;
  1117. int namelen = dentry->d_name.len;
  1118. unsigned long offset = 0;
  1119. unsigned short reclen;
  1120. int nlen, rlen, err;
  1121. char *top;
  1122. reclen = EXT3_DIR_REC_LEN(namelen);
  1123. if (!de) {
  1124. de = (struct ext3_dir_entry_2 *)bh->b_data;
  1125. top = bh->b_data + dir->i_sb->s_blocksize - reclen;
  1126. while ((char *) de <= top) {
  1127. if (!ext3_check_dir_entry("ext3_add_entry", dir, de,
  1128. bh, offset)) {
  1129. brelse (bh);
  1130. return -EIO;
  1131. }
  1132. if (ext3_match (namelen, name, de)) {
  1133. brelse (bh);
  1134. return -EEXIST;
  1135. }
  1136. nlen = EXT3_DIR_REC_LEN(de->name_len);
  1137. rlen = le16_to_cpu(de->rec_len);
  1138. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1139. break;
  1140. de = (struct ext3_dir_entry_2 *)((char *)de + rlen);
  1141. offset += rlen;
  1142. }
  1143. if ((char *) de > top)
  1144. return -ENOSPC;
  1145. }
  1146. BUFFER_TRACE(bh, "get_write_access");
  1147. err = ext3_journal_get_write_access(handle, bh);
  1148. if (err) {
  1149. ext3_std_error(dir->i_sb, err);
  1150. brelse(bh);
  1151. return err;
  1152. }
  1153. /* By now the buffer is marked for journaling */
  1154. nlen = EXT3_DIR_REC_LEN(de->name_len);
  1155. rlen = le16_to_cpu(de->rec_len);
  1156. if (de->inode) {
  1157. struct ext3_dir_entry_2 *de1 = (struct ext3_dir_entry_2 *)((char *)de + nlen);
  1158. de1->rec_len = cpu_to_le16(rlen - nlen);
  1159. de->rec_len = cpu_to_le16(nlen);
  1160. de = de1;
  1161. }
  1162. de->file_type = EXT3_FT_UNKNOWN;
  1163. if (inode) {
  1164. de->inode = cpu_to_le32(inode->i_ino);
  1165. ext3_set_de_type(dir->i_sb, de, inode->i_mode);
  1166. } else
  1167. de->inode = 0;
  1168. de->name_len = namelen;
  1169. memcpy (de->name, name, namelen);
  1170. /*
  1171. * XXX shouldn't update any times until successful
  1172. * completion of syscall, but too many callers depend
  1173. * on this.
  1174. *
  1175. * XXX similarly, too many callers depend on
  1176. * ext3_new_inode() setting the times, but error
  1177. * recovery deletes the inode, so the worst that can
  1178. * happen is that the times are slightly out of date
  1179. * and/or different from the directory change time.
  1180. */
  1181. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  1182. ext3_update_dx_flag(dir);
  1183. dir->i_version++;
  1184. ext3_mark_inode_dirty(handle, dir);
  1185. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  1186. err = ext3_journal_dirty_metadata(handle, bh);
  1187. if (err)
  1188. ext3_std_error(dir->i_sb, err);
  1189. brelse(bh);
  1190. return 0;
  1191. }
  1192. #ifdef CONFIG_EXT3_INDEX
  1193. /*
  1194. * This converts a one block unindexed directory to a 3 block indexed
  1195. * directory, and adds the dentry to the indexed directory.
  1196. */
  1197. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1198. struct inode *inode, struct buffer_head *bh)
  1199. {
  1200. struct inode *dir = dentry->d_parent->d_inode;
  1201. const char *name = dentry->d_name.name;
  1202. int namelen = dentry->d_name.len;
  1203. struct buffer_head *bh2;
  1204. struct dx_root *root;
  1205. struct dx_frame frames[2], *frame;
  1206. struct dx_entry *entries;
  1207. struct ext3_dir_entry_2 *de, *de2;
  1208. char *data1, *top;
  1209. unsigned len;
  1210. int retval;
  1211. unsigned blocksize;
  1212. struct dx_hash_info hinfo;
  1213. u32 block;
  1214. struct fake_dirent *fde;
  1215. blocksize = dir->i_sb->s_blocksize;
  1216. dxtrace(printk("Creating index\n"));
  1217. retval = ext3_journal_get_write_access(handle, bh);
  1218. if (retval) {
  1219. ext3_std_error(dir->i_sb, retval);
  1220. brelse(bh);
  1221. return retval;
  1222. }
  1223. root = (struct dx_root *) bh->b_data;
  1224. bh2 = ext3_append (handle, dir, &block, &retval);
  1225. if (!(bh2)) {
  1226. brelse(bh);
  1227. return retval;
  1228. }
  1229. EXT3_I(dir)->i_flags |= EXT3_INDEX_FL;
  1230. data1 = bh2->b_data;
  1231. /* The 0th block becomes the root, move the dirents out */
  1232. fde = &root->dotdot;
  1233. de = (struct ext3_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len));
  1234. len = ((char *) root) + blocksize - (char *) de;
  1235. memcpy (data1, de, len);
  1236. de = (struct ext3_dir_entry_2 *) data1;
  1237. top = data1 + len;
  1238. while ((char *)(de2=(void*)de+le16_to_cpu(de->rec_len)) < top)
  1239. de = de2;
  1240. de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
  1241. /* Initialize the root; the dot dirents already exist */
  1242. de = (struct ext3_dir_entry_2 *) (&root->dotdot);
  1243. de->rec_len = cpu_to_le16(blocksize - EXT3_DIR_REC_LEN(2));
  1244. memset (&root->info, 0, sizeof(root->info));
  1245. root->info.info_length = sizeof(root->info);
  1246. root->info.hash_version = EXT3_SB(dir->i_sb)->s_def_hash_version;
  1247. entries = root->entries;
  1248. dx_set_block (entries, 1);
  1249. dx_set_count (entries, 1);
  1250. dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
  1251. /* Initialize as for dx_probe */
  1252. hinfo.hash_version = root->info.hash_version;
  1253. hinfo.seed = EXT3_SB(dir->i_sb)->s_hash_seed;
  1254. ext3fs_dirhash(name, namelen, &hinfo);
  1255. frame = frames;
  1256. frame->entries = entries;
  1257. frame->at = entries;
  1258. frame->bh = bh;
  1259. bh = bh2;
  1260. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1261. dx_release (frames);
  1262. if (!(de))
  1263. return retval;
  1264. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1265. }
  1266. #endif
  1267. /*
  1268. * ext3_add_entry()
  1269. *
  1270. * adds a file entry to the specified directory, using the same
  1271. * semantics as ext3_find_entry(). It returns NULL if it failed.
  1272. *
  1273. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1274. * may not sleep between calling this and putting something into
  1275. * the entry, as someone else might have used it while you slept.
  1276. */
  1277. static int ext3_add_entry (handle_t *handle, struct dentry *dentry,
  1278. struct inode *inode)
  1279. {
  1280. struct inode *dir = dentry->d_parent->d_inode;
  1281. unsigned long offset;
  1282. struct buffer_head * bh;
  1283. struct ext3_dir_entry_2 *de;
  1284. struct super_block * sb;
  1285. int retval;
  1286. #ifdef CONFIG_EXT3_INDEX
  1287. int dx_fallback=0;
  1288. #endif
  1289. unsigned blocksize;
  1290. u32 block, blocks;
  1291. sb = dir->i_sb;
  1292. blocksize = sb->s_blocksize;
  1293. if (!dentry->d_name.len)
  1294. return -EINVAL;
  1295. #ifdef CONFIG_EXT3_INDEX
  1296. if (is_dx(dir)) {
  1297. retval = ext3_dx_add_entry(handle, dentry, inode);
  1298. if (!retval || (retval != ERR_BAD_DX_DIR))
  1299. return retval;
  1300. EXT3_I(dir)->i_flags &= ~EXT3_INDEX_FL;
  1301. dx_fallback++;
  1302. ext3_mark_inode_dirty(handle, dir);
  1303. }
  1304. #endif
  1305. blocks = dir->i_size >> sb->s_blocksize_bits;
  1306. for (block = 0, offset = 0; block < blocks; block++) {
  1307. bh = ext3_bread(handle, dir, block, 0, &retval);
  1308. if(!bh)
  1309. return retval;
  1310. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1311. if (retval != -ENOSPC)
  1312. return retval;
  1313. #ifdef CONFIG_EXT3_INDEX
  1314. if (blocks == 1 && !dx_fallback &&
  1315. EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
  1316. return make_indexed_dir(handle, dentry, inode, bh);
  1317. #endif
  1318. brelse(bh);
  1319. }
  1320. bh = ext3_append(handle, dir, &block, &retval);
  1321. if (!bh)
  1322. return retval;
  1323. de = (struct ext3_dir_entry_2 *) bh->b_data;
  1324. de->inode = 0;
  1325. de->rec_len = cpu_to_le16(blocksize);
  1326. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1327. }
  1328. #ifdef CONFIG_EXT3_INDEX
  1329. /*
  1330. * Returns 0 for success, or a negative error value
  1331. */
  1332. static int ext3_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1333. struct inode *inode)
  1334. {
  1335. struct dx_frame frames[2], *frame;
  1336. struct dx_entry *entries, *at;
  1337. struct dx_hash_info hinfo;
  1338. struct buffer_head * bh;
  1339. struct inode *dir = dentry->d_parent->d_inode;
  1340. struct super_block * sb = dir->i_sb;
  1341. struct ext3_dir_entry_2 *de;
  1342. int err;
  1343. frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
  1344. if (!frame)
  1345. return err;
  1346. entries = frame->entries;
  1347. at = frame->at;
  1348. if (!(bh = ext3_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1349. goto cleanup;
  1350. BUFFER_TRACE(bh, "get_write_access");
  1351. err = ext3_journal_get_write_access(handle, bh);
  1352. if (err)
  1353. goto journal_error;
  1354. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1355. if (err != -ENOSPC) {
  1356. bh = NULL;
  1357. goto cleanup;
  1358. }
  1359. /* Block full, should compress but for now just split */
  1360. dxtrace(printk("using %u of %u node entries\n",
  1361. dx_get_count(entries), dx_get_limit(entries)));
  1362. /* Need to split index? */
  1363. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1364. u32 newblock;
  1365. unsigned icount = dx_get_count(entries);
  1366. int levels = frame - frames;
  1367. struct dx_entry *entries2;
  1368. struct dx_node *node2;
  1369. struct buffer_head *bh2;
  1370. if (levels && (dx_get_count(frames->entries) ==
  1371. dx_get_limit(frames->entries))) {
  1372. ext3_warning(sb, __FUNCTION__,
  1373. "Directory index full!");
  1374. err = -ENOSPC;
  1375. goto cleanup;
  1376. }
  1377. bh2 = ext3_append (handle, dir, &newblock, &err);
  1378. if (!(bh2))
  1379. goto cleanup;
  1380. node2 = (struct dx_node *)(bh2->b_data);
  1381. entries2 = node2->entries;
  1382. node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
  1383. node2->fake.inode = 0;
  1384. BUFFER_TRACE(frame->bh, "get_write_access");
  1385. err = ext3_journal_get_write_access(handle, frame->bh);
  1386. if (err)
  1387. goto journal_error;
  1388. if (levels) {
  1389. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1390. unsigned hash2 = dx_get_hash(entries + icount1);
  1391. dxtrace(printk("Split index %i/%i\n", icount1, icount2));
  1392. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1393. err = ext3_journal_get_write_access(handle,
  1394. frames[0].bh);
  1395. if (err)
  1396. goto journal_error;
  1397. memcpy ((char *) entries2, (char *) (entries + icount1),
  1398. icount2 * sizeof(struct dx_entry));
  1399. dx_set_count (entries, icount1);
  1400. dx_set_count (entries2, icount2);
  1401. dx_set_limit (entries2, dx_node_limit(dir));
  1402. /* Which index block gets the new entry? */
  1403. if (at - entries >= icount1) {
  1404. frame->at = at = at - entries - icount1 + entries2;
  1405. frame->entries = entries = entries2;
  1406. swap(frame->bh, bh2);
  1407. }
  1408. dx_insert_block (frames + 0, hash2, newblock);
  1409. dxtrace(dx_show_index ("node", frames[1].entries));
  1410. dxtrace(dx_show_index ("node",
  1411. ((struct dx_node *) bh2->b_data)->entries));
  1412. err = ext3_journal_dirty_metadata(handle, bh2);
  1413. if (err)
  1414. goto journal_error;
  1415. brelse (bh2);
  1416. } else {
  1417. dxtrace(printk("Creating second level index...\n"));
  1418. memcpy((char *) entries2, (char *) entries,
  1419. icount * sizeof(struct dx_entry));
  1420. dx_set_limit(entries2, dx_node_limit(dir));
  1421. /* Set up root */
  1422. dx_set_count(entries, 1);
  1423. dx_set_block(entries + 0, newblock);
  1424. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1425. /* Add new access path frame */
  1426. frame = frames + 1;
  1427. frame->at = at = at - entries + entries2;
  1428. frame->entries = entries = entries2;
  1429. frame->bh = bh2;
  1430. err = ext3_journal_get_write_access(handle,
  1431. frame->bh);
  1432. if (err)
  1433. goto journal_error;
  1434. }
  1435. ext3_journal_dirty_metadata(handle, frames[0].bh);
  1436. }
  1437. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1438. if (!de)
  1439. goto cleanup;
  1440. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1441. bh = NULL;
  1442. goto cleanup;
  1443. journal_error:
  1444. ext3_std_error(dir->i_sb, err);
  1445. cleanup:
  1446. if (bh)
  1447. brelse(bh);
  1448. dx_release(frames);
  1449. return err;
  1450. }
  1451. #endif
  1452. /*
  1453. * ext3_delete_entry deletes a directory entry by merging it with the
  1454. * previous entry
  1455. */
  1456. static int ext3_delete_entry (handle_t *handle,
  1457. struct inode * dir,
  1458. struct ext3_dir_entry_2 * de_del,
  1459. struct buffer_head * bh)
  1460. {
  1461. struct ext3_dir_entry_2 * de, * pde;
  1462. int i;
  1463. i = 0;
  1464. pde = NULL;
  1465. de = (struct ext3_dir_entry_2 *) bh->b_data;
  1466. while (i < bh->b_size) {
  1467. if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))
  1468. return -EIO;
  1469. if (de == de_del) {
  1470. BUFFER_TRACE(bh, "get_write_access");
  1471. ext3_journal_get_write_access(handle, bh);
  1472. if (pde)
  1473. pde->rec_len =
  1474. cpu_to_le16(le16_to_cpu(pde->rec_len) +
  1475. le16_to_cpu(de->rec_len));
  1476. else
  1477. de->inode = 0;
  1478. dir->i_version++;
  1479. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  1480. ext3_journal_dirty_metadata(handle, bh);
  1481. return 0;
  1482. }
  1483. i += le16_to_cpu(de->rec_len);
  1484. pde = de;
  1485. de = (struct ext3_dir_entry_2 *)
  1486. ((char *) de + le16_to_cpu(de->rec_len));
  1487. }
  1488. return -ENOENT;
  1489. }
  1490. /*
  1491. * ext3_mark_inode_dirty is somewhat expensive, so unlike ext2 we
  1492. * do not perform it in these functions. We perform it at the call site,
  1493. * if it is needed.
  1494. */
  1495. static inline void ext3_inc_count(handle_t *handle, struct inode *inode)
  1496. {
  1497. inc_nlink(inode);
  1498. }
  1499. static inline void ext3_dec_count(handle_t *handle, struct inode *inode)
  1500. {
  1501. drop_nlink(inode);
  1502. }
  1503. static int ext3_add_nondir(handle_t *handle,
  1504. struct dentry *dentry, struct inode *inode)
  1505. {
  1506. int err = ext3_add_entry(handle, dentry, inode);
  1507. if (!err) {
  1508. ext3_mark_inode_dirty(handle, inode);
  1509. d_instantiate(dentry, inode);
  1510. return 0;
  1511. }
  1512. ext3_dec_count(handle, inode);
  1513. iput(inode);
  1514. return err;
  1515. }
  1516. /*
  1517. * By the time this is called, we already have created
  1518. * the directory cache entry for the new file, but it
  1519. * is so far negative - it has no inode.
  1520. *
  1521. * If the create succeeds, we fill in the inode information
  1522. * with d_instantiate().
  1523. */
  1524. static int ext3_create (struct inode * dir, struct dentry * dentry, int mode,
  1525. struct nameidata *nd)
  1526. {
  1527. handle_t *handle;
  1528. struct inode * inode;
  1529. int err, retries = 0;
  1530. retry:
  1531. handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
  1532. EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1533. 2*EXT3_QUOTA_INIT_BLOCKS(dir->i_sb));
  1534. if (IS_ERR(handle))
  1535. return PTR_ERR(handle);
  1536. if (IS_DIRSYNC(dir))
  1537. handle->h_sync = 1;
  1538. inode = ext3_new_inode (handle, dir, mode);
  1539. err = PTR_ERR(inode);
  1540. if (!IS_ERR(inode)) {
  1541. inode->i_op = &ext3_file_inode_operations;
  1542. inode->i_fop = &ext3_file_operations;
  1543. ext3_set_aops(inode);
  1544. err = ext3_add_nondir(handle, dentry, inode);
  1545. }
  1546. ext3_journal_stop(handle);
  1547. if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
  1548. goto retry;
  1549. return err;
  1550. }
  1551. static int ext3_mknod (struct inode * dir, struct dentry *dentry,
  1552. int mode, dev_t rdev)
  1553. {
  1554. handle_t *handle;
  1555. struct inode *inode;
  1556. int err, retries = 0;
  1557. if (!new_valid_dev(rdev))
  1558. return -EINVAL;
  1559. retry:
  1560. handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
  1561. EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1562. 2*EXT3_QUOTA_INIT_BLOCKS(dir->i_sb));
  1563. if (IS_ERR(handle))
  1564. return PTR_ERR(handle);
  1565. if (IS_DIRSYNC(dir))
  1566. handle->h_sync = 1;
  1567. inode = ext3_new_inode (handle, dir, mode);
  1568. err = PTR_ERR(inode);
  1569. if (!IS_ERR(inode)) {
  1570. init_special_inode(inode, inode->i_mode, rdev);
  1571. #ifdef CONFIG_EXT3_FS_XATTR
  1572. inode->i_op = &ext3_special_inode_operations;
  1573. #endif
  1574. err = ext3_add_nondir(handle, dentry, inode);
  1575. }
  1576. ext3_journal_stop(handle);
  1577. if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
  1578. goto retry;
  1579. return err;
  1580. }
  1581. static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode)
  1582. {
  1583. handle_t *handle;
  1584. struct inode * inode;
  1585. struct buffer_head * dir_block;
  1586. struct ext3_dir_entry_2 * de;
  1587. int err, retries = 0;
  1588. if (dir->i_nlink >= EXT3_LINK_MAX)
  1589. return -EMLINK;
  1590. retry:
  1591. handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
  1592. EXT3_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1593. 2*EXT3_QUOTA_INIT_BLOCKS(dir->i_sb));
  1594. if (IS_ERR(handle))
  1595. return PTR_ERR(handle);
  1596. if (IS_DIRSYNC(dir))
  1597. handle->h_sync = 1;
  1598. inode = ext3_new_inode (handle, dir, S_IFDIR | mode);
  1599. err = PTR_ERR(inode);
  1600. if (IS_ERR(inode))
  1601. goto out_stop;
  1602. inode->i_op = &ext3_dir_inode_operations;
  1603. inode->i_fop = &ext3_dir_operations;
  1604. inode->i_size = EXT3_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1605. dir_block = ext3_bread (handle, inode, 0, 1, &err);
  1606. if (!dir_block) {
  1607. drop_nlink(inode); /* is this nlink == 0? */
  1608. ext3_mark_inode_dirty(handle, inode);
  1609. iput (inode);
  1610. goto out_stop;
  1611. }
  1612. BUFFER_TRACE(dir_block, "get_write_access");
  1613. ext3_journal_get_write_access(handle, dir_block);
  1614. de = (struct ext3_dir_entry_2 *) dir_block->b_data;
  1615. de->inode = cpu_to_le32(inode->i_ino);
  1616. de->name_len = 1;
  1617. de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de->name_len));
  1618. strcpy (de->name, ".");
  1619. ext3_set_de_type(dir->i_sb, de, S_IFDIR);
  1620. de = (struct ext3_dir_entry_2 *)
  1621. ((char *) de + le16_to_cpu(de->rec_len));
  1622. de->inode = cpu_to_le32(dir->i_ino);
  1623. de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT3_DIR_REC_LEN(1));
  1624. de->name_len = 2;
  1625. strcpy (de->name, "..");
  1626. ext3_set_de_type(dir->i_sb, de, S_IFDIR);
  1627. inode->i_nlink = 2;
  1628. BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");
  1629. ext3_journal_dirty_metadata(handle, dir_block);
  1630. brelse (dir_block);
  1631. ext3_mark_inode_dirty(handle, inode);
  1632. err = ext3_add_entry (handle, dentry, inode);
  1633. if (err) {
  1634. inode->i_nlink = 0;
  1635. ext3_mark_inode_dirty(handle, inode);
  1636. iput (inode);
  1637. goto out_stop;
  1638. }
  1639. inc_nlink(dir);
  1640. ext3_update_dx_flag(dir);
  1641. ext3_mark_inode_dirty(handle, dir);
  1642. d_instantiate(dentry, inode);
  1643. out_stop:
  1644. ext3_journal_stop(handle);
  1645. if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
  1646. goto retry;
  1647. return err;
  1648. }
  1649. /*
  1650. * routine to check that the specified directory is empty (for rmdir)
  1651. */
  1652. static int empty_dir (struct inode * inode)
  1653. {
  1654. unsigned long offset;
  1655. struct buffer_head * bh;
  1656. struct ext3_dir_entry_2 * de, * de1;
  1657. struct super_block * sb;
  1658. int err = 0;
  1659. sb = inode->i_sb;
  1660. if (inode->i_size < EXT3_DIR_REC_LEN(1) + EXT3_DIR_REC_LEN(2) ||
  1661. !(bh = ext3_bread (NULL, inode, 0, 0, &err))) {
  1662. if (err)
  1663. ext3_error(inode->i_sb, __FUNCTION__,
  1664. "error %d reading directory #%lu offset 0",
  1665. err, inode->i_ino);
  1666. else
  1667. ext3_warning(inode->i_sb, __FUNCTION__,
  1668. "bad directory (dir #%lu) - no data block",
  1669. inode->i_ino);
  1670. return 1;
  1671. }
  1672. de = (struct ext3_dir_entry_2 *) bh->b_data;
  1673. de1 = (struct ext3_dir_entry_2 *)
  1674. ((char *) de + le16_to_cpu(de->rec_len));
  1675. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1676. !le32_to_cpu(de1->inode) ||
  1677. strcmp (".", de->name) ||
  1678. strcmp ("..", de1->name)) {
  1679. ext3_warning (inode->i_sb, "empty_dir",
  1680. "bad directory (dir #%lu) - no `.' or `..'",
  1681. inode->i_ino);
  1682. brelse (bh);
  1683. return 1;
  1684. }
  1685. offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
  1686. de = (struct ext3_dir_entry_2 *)
  1687. ((char *) de1 + le16_to_cpu(de1->rec_len));
  1688. while (offset < inode->i_size ) {
  1689. if (!bh ||
  1690. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1691. err = 0;
  1692. brelse (bh);
  1693. bh = ext3_bread (NULL, inode,
  1694. offset >> EXT3_BLOCK_SIZE_BITS(sb), 0, &err);
  1695. if (!bh) {
  1696. if (err)
  1697. ext3_error(sb, __FUNCTION__,
  1698. "error %d reading directory"
  1699. " #%lu offset %lu",
  1700. err, inode->i_ino, offset);
  1701. offset += sb->s_blocksize;
  1702. continue;
  1703. }
  1704. de = (struct ext3_dir_entry_2 *) bh->b_data;
  1705. }
  1706. if (!ext3_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1707. de = (struct ext3_dir_entry_2 *)(bh->b_data +
  1708. sb->s_blocksize);
  1709. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1710. continue;
  1711. }
  1712. if (le32_to_cpu(de->inode)) {
  1713. brelse (bh);
  1714. return 0;
  1715. }
  1716. offset += le16_to_cpu(de->rec_len);
  1717. de = (struct ext3_dir_entry_2 *)
  1718. ((char *) de + le16_to_cpu(de->rec_len));
  1719. }
  1720. brelse (bh);
  1721. return 1;
  1722. }
  1723. /* ext3_orphan_add() links an unlinked or truncated inode into a list of
  1724. * such inodes, starting at the superblock, in case we crash before the
  1725. * file is closed/deleted, or in case the inode truncate spans multiple
  1726. * transactions and the last transaction is not recovered after a crash.
  1727. *
  1728. * At filesystem recovery time, we walk this list deleting unlinked
  1729. * inodes and truncating linked inodes in ext3_orphan_cleanup().
  1730. */
  1731. int ext3_orphan_add(handle_t *handle, struct inode *inode)
  1732. {
  1733. struct super_block *sb = inode->i_sb;
  1734. struct ext3_iloc iloc;
  1735. int err = 0, rc;
  1736. lock_super(sb);
  1737. if (!list_empty(&EXT3_I(inode)->i_orphan))
  1738. goto out_unlock;
  1739. /* Orphan handling is only valid for files with data blocks
  1740. * being truncated, or files being unlinked. */
  1741. /* @@@ FIXME: Observation from aviro:
  1742. * I think I can trigger J_ASSERT in ext3_orphan_add(). We block
  1743. * here (on lock_super()), so race with ext3_link() which might bump
  1744. * ->i_nlink. For, say it, character device. Not a regular file,
  1745. * not a directory, not a symlink and ->i_nlink > 0.
  1746. */
  1747. J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1748. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1749. BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "get_write_access");
  1750. err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
  1751. if (err)
  1752. goto out_unlock;
  1753. err = ext3_reserve_inode_write(handle, inode, &iloc);
  1754. if (err)
  1755. goto out_unlock;
  1756. /* Insert this inode at the head of the on-disk orphan list... */
  1757. NEXT_ORPHAN(inode) = le32_to_cpu(EXT3_SB(sb)->s_es->s_last_orphan);
  1758. EXT3_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1759. err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  1760. rc = ext3_mark_iloc_dirty(handle, inode, &iloc);
  1761. if (!err)
  1762. err = rc;
  1763. /* Only add to the head of the in-memory list if all the
  1764. * previous operations succeeded. If the orphan_add is going to
  1765. * fail (possibly taking the journal offline), we can't risk
  1766. * leaving the inode on the orphan list: stray orphan-list
  1767. * entries can cause panics at unmount time.
  1768. *
  1769. * This is safe: on error we're going to ignore the orphan list
  1770. * anyway on the next recovery. */
  1771. if (!err)
  1772. list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
  1773. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1774. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1775. inode->i_ino, NEXT_ORPHAN(inode));
  1776. out_unlock:
  1777. unlock_super(sb);
  1778. ext3_std_error(inode->i_sb, err);
  1779. return err;
  1780. }
  1781. /*
  1782. * ext3_orphan_del() removes an unlinked or truncated inode from the list
  1783. * of such inodes stored on disk, because it is finally being cleaned up.
  1784. */
  1785. int ext3_orphan_del(handle_t *handle, struct inode *inode)
  1786. {
  1787. struct list_head *prev;
  1788. struct ext3_inode_info *ei = EXT3_I(inode);
  1789. struct ext3_sb_info *sbi;
  1790. unsigned long ino_next;
  1791. struct ext3_iloc iloc;
  1792. int err = 0;
  1793. lock_super(inode->i_sb);
  1794. if (list_empty(&ei->i_orphan)) {
  1795. unlock_super(inode->i_sb);
  1796. return 0;
  1797. }
  1798. ino_next = NEXT_ORPHAN(inode);
  1799. prev = ei->i_orphan.prev;
  1800. sbi = EXT3_SB(inode->i_sb);
  1801. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1802. list_del_init(&ei->i_orphan);
  1803. /* If we're on an error path, we may not have a valid
  1804. * transaction handle with which to update the orphan list on
  1805. * disk, but we still need to remove the inode from the linked
  1806. * list in memory. */
  1807. if (!handle)
  1808. goto out;
  1809. err = ext3_reserve_inode_write(handle, inode, &iloc);
  1810. if (err)
  1811. goto out_err;
  1812. if (prev == &sbi->s_orphan) {
  1813. jbd_debug(4, "superblock will point to %lu\n", ino_next);
  1814. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1815. err = ext3_journal_get_write_access(handle, sbi->s_sbh);
  1816. if (err)
  1817. goto out_brelse;
  1818. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1819. err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
  1820. } else {
  1821. struct ext3_iloc iloc2;
  1822. struct inode *i_prev =
  1823. &list_entry(prev, struct ext3_inode_info, i_orphan)->vfs_inode;
  1824. jbd_debug(4, "orphan inode %lu will point to %lu\n",
  1825. i_prev->i_ino, ino_next);
  1826. err = ext3_reserve_inode_write(handle, i_prev, &iloc2);
  1827. if (err)
  1828. goto out_brelse;
  1829. NEXT_ORPHAN(i_prev) = ino_next;
  1830. err = ext3_mark_iloc_dirty(handle, i_prev, &iloc2);
  1831. }
  1832. if (err)
  1833. goto out_brelse;
  1834. NEXT_ORPHAN(inode) = 0;
  1835. err = ext3_mark_iloc_dirty(handle, inode, &iloc);
  1836. out_err:
  1837. ext3_std_error(inode->i_sb, err);
  1838. out:
  1839. unlock_super(inode->i_sb);
  1840. return err;
  1841. out_brelse:
  1842. brelse(iloc.bh);
  1843. goto out_err;
  1844. }
  1845. static int ext3_rmdir (struct inode * dir, struct dentry *dentry)
  1846. {
  1847. int retval;
  1848. struct inode * inode;
  1849. struct buffer_head * bh;
  1850. struct ext3_dir_entry_2 * de;
  1851. handle_t *handle;
  1852. /* Initialize quotas before so that eventual writes go in
  1853. * separate transaction */
  1854. DQUOT_INIT(dentry->d_inode);
  1855. handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb));
  1856. if (IS_ERR(handle))
  1857. return PTR_ERR(handle);
  1858. retval = -ENOENT;
  1859. bh = ext3_find_entry (dentry, &de);
  1860. if (!bh)
  1861. goto end_rmdir;
  1862. if (IS_DIRSYNC(dir))
  1863. handle->h_sync = 1;
  1864. inode = dentry->d_inode;
  1865. retval = -EIO;
  1866. if (le32_to_cpu(de->inode) != inode->i_ino)
  1867. goto end_rmdir;
  1868. retval = -ENOTEMPTY;
  1869. if (!empty_dir (inode))
  1870. goto end_rmdir;
  1871. retval = ext3_delete_entry(handle, dir, de, bh);
  1872. if (retval)
  1873. goto end_rmdir;
  1874. if (inode->i_nlink != 2)
  1875. ext3_warning (inode->i_sb, "ext3_rmdir",
  1876. "empty directory has nlink!=2 (%d)",
  1877. inode->i_nlink);
  1878. inode->i_version++;
  1879. clear_nlink(inode);
  1880. /* There's no need to set i_disksize: the fact that i_nlink is
  1881. * zero will ensure that the right thing happens during any
  1882. * recovery. */
  1883. inode->i_size = 0;
  1884. ext3_orphan_add(handle, inode);
  1885. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
  1886. ext3_mark_inode_dirty(handle, inode);
  1887. drop_nlink(dir);
  1888. ext3_update_dx_flag(dir);
  1889. ext3_mark_inode_dirty(handle, dir);
  1890. end_rmdir:
  1891. ext3_journal_stop(handle);
  1892. brelse (bh);
  1893. return retval;
  1894. }
  1895. static int ext3_unlink(struct inode * dir, struct dentry *dentry)
  1896. {
  1897. int retval;
  1898. struct inode * inode;
  1899. struct buffer_head * bh;
  1900. struct ext3_dir_entry_2 * de;
  1901. handle_t *handle;
  1902. /* Initialize quotas before so that eventual writes go
  1903. * in separate transaction */
  1904. DQUOT_INIT(dentry->d_inode);
  1905. handle = ext3_journal_start(dir, EXT3_DELETE_TRANS_BLOCKS(dir->i_sb));
  1906. if (IS_ERR(handle))
  1907. return PTR_ERR(handle);
  1908. if (IS_DIRSYNC(dir))
  1909. handle->h_sync = 1;
  1910. retval = -ENOENT;
  1911. bh = ext3_find_entry (dentry, &de);
  1912. if (!bh)
  1913. goto end_unlink;
  1914. inode = dentry->d_inode;
  1915. retval = -EIO;
  1916. if (le32_to_cpu(de->inode) != inode->i_ino)
  1917. goto end_unlink;
  1918. if (!inode->i_nlink) {
  1919. ext3_warning (inode->i_sb, "ext3_unlink",
  1920. "Deleting nonexistent file (%lu), %d",
  1921. inode->i_ino, inode->i_nlink);
  1922. inode->i_nlink = 1;
  1923. }
  1924. retval = ext3_delete_entry(handle, dir, de, bh);
  1925. if (retval)
  1926. goto end_unlink;
  1927. dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
  1928. ext3_update_dx_flag(dir);
  1929. ext3_mark_inode_dirty(handle, dir);
  1930. drop_nlink(inode);
  1931. if (!inode->i_nlink)
  1932. ext3_orphan_add(handle, inode);
  1933. inode->i_ctime = dir->i_ctime;
  1934. ext3_mark_inode_dirty(handle, inode);
  1935. retval = 0;
  1936. end_unlink:
  1937. ext3_journal_stop(handle);
  1938. brelse (bh);
  1939. return retval;
  1940. }
  1941. static int ext3_symlink (struct inode * dir,
  1942. struct dentry *dentry, const char * symname)
  1943. {
  1944. handle_t *handle;
  1945. struct inode * inode;
  1946. int l, err, retries = 0;
  1947. l = strlen(symname)+1;
  1948. if (l > dir->i_sb->s_blocksize)
  1949. return -ENAMETOOLONG;
  1950. retry:
  1951. handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
  1952. EXT3_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  1953. 2*EXT3_QUOTA_INIT_BLOCKS(dir->i_sb));
  1954. if (IS_ERR(handle))
  1955. return PTR_ERR(handle);
  1956. if (IS_DIRSYNC(dir))
  1957. handle->h_sync = 1;
  1958. inode = ext3_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
  1959. err = PTR_ERR(inode);
  1960. if (IS_ERR(inode))
  1961. goto out_stop;
  1962. if (l > sizeof (EXT3_I(inode)->i_data)) {
  1963. inode->i_op = &ext3_symlink_inode_operations;
  1964. ext3_set_aops(inode);
  1965. /*
  1966. * page_symlink() calls into ext3_prepare/commit_write.
  1967. * We have a transaction open. All is sweetness. It also sets
  1968. * i_size in generic_commit_write().
  1969. */
  1970. err = __page_symlink(inode, symname, l,
  1971. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  1972. if (err) {
  1973. ext3_dec_count(handle, inode);
  1974. ext3_mark_inode_dirty(handle, inode);
  1975. iput (inode);
  1976. goto out_stop;
  1977. }
  1978. } else {
  1979. inode->i_op = &ext3_fast_symlink_inode_operations;
  1980. memcpy((char*)&EXT3_I(inode)->i_data,symname,l);
  1981. inode->i_size = l-1;
  1982. }
  1983. EXT3_I(inode)->i_disksize = inode->i_size;
  1984. err = ext3_add_nondir(handle, dentry, inode);
  1985. out_stop:
  1986. ext3_journal_stop(handle);
  1987. if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
  1988. goto retry;
  1989. return err;
  1990. }
  1991. static int ext3_link (struct dentry * old_dentry,
  1992. struct inode * dir, struct dentry *dentry)
  1993. {
  1994. handle_t *handle;
  1995. struct inode *inode = old_dentry->d_inode;
  1996. int err, retries = 0;
  1997. if (inode->i_nlink >= EXT3_LINK_MAX)
  1998. return -EMLINK;
  1999. retry:
  2000. handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS(dir->i_sb) +
  2001. EXT3_INDEX_EXTRA_TRANS_BLOCKS);
  2002. if (IS_ERR(handle))
  2003. return PTR_ERR(handle);
  2004. if (IS_DIRSYNC(dir))
  2005. handle->h_sync = 1;
  2006. inode->i_ctime = CURRENT_TIME_SEC;
  2007. ext3_inc_count(handle, inode);
  2008. atomic_inc(&inode->i_count);
  2009. err = ext3_add_nondir(handle, dentry, inode);
  2010. ext3_journal_stop(handle);
  2011. if (err == -ENOSPC && ext3_should_retry_alloc(dir->i_sb, &retries))
  2012. goto retry;
  2013. return err;
  2014. }
  2015. #define PARENT_INO(buffer) \
  2016. ((struct ext3_dir_entry_2 *) ((char *) buffer + \
  2017. le16_to_cpu(((struct ext3_dir_entry_2 *) buffer)->rec_len)))->inode
  2018. /*
  2019. * Anybody can rename anything with this: the permission checks are left to the
  2020. * higher-level routines.
  2021. */
  2022. static int ext3_rename (struct inode * old_dir, struct dentry *old_dentry,
  2023. struct inode * new_dir,struct dentry *new_dentry)
  2024. {
  2025. handle_t *handle;
  2026. struct inode * old_inode, * new_inode;
  2027. struct buffer_head * old_bh, * new_bh, * dir_bh;
  2028. struct ext3_dir_entry_2 * old_de, * new_de;
  2029. int retval;
  2030. old_bh = new_bh = dir_bh = NULL;
  2031. /* Initialize quotas before so that eventual writes go
  2032. * in separate transaction */
  2033. if (new_dentry->d_inode)
  2034. DQUOT_INIT(new_dentry->d_inode);
  2035. handle = ext3_journal_start(old_dir, 2 *
  2036. EXT3_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2037. EXT3_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2038. if (IS_ERR(handle))
  2039. return PTR_ERR(handle);
  2040. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2041. handle->h_sync = 1;
  2042. old_bh = ext3_find_entry (old_dentry, &old_de);
  2043. /*
  2044. * Check for inode number is _not_ due to possible IO errors.
  2045. * We might rmdir the source, keep it as pwd of some process
  2046. * and merrily kill the link to whatever was created under the
  2047. * same name. Goodbye sticky bit ;-<
  2048. */
  2049. old_inode = old_dentry->d_inode;
  2050. retval = -ENOENT;
  2051. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2052. goto end_rename;
  2053. new_inode = new_dentry->d_inode;
  2054. new_bh = ext3_find_entry (new_dentry, &new_de);
  2055. if (new_bh) {
  2056. if (!new_inode) {
  2057. brelse (new_bh);
  2058. new_bh = NULL;
  2059. }
  2060. }
  2061. if (S_ISDIR(old_inode->i_mode)) {
  2062. if (new_inode) {
  2063. retval = -ENOTEMPTY;
  2064. if (!empty_dir (new_inode))
  2065. goto end_rename;
  2066. }
  2067. retval = -EIO;
  2068. dir_bh = ext3_bread (handle, old_inode, 0, 0, &retval);
  2069. if (!dir_bh)
  2070. goto end_rename;
  2071. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2072. goto end_rename;
  2073. retval = -EMLINK;
  2074. if (!new_inode && new_dir!=old_dir &&
  2075. new_dir->i_nlink >= EXT3_LINK_MAX)
  2076. goto end_rename;
  2077. }
  2078. if (!new_bh) {
  2079. retval = ext3_add_entry (handle, new_dentry, old_inode);
  2080. if (retval)
  2081. goto end_rename;
  2082. } else {
  2083. BUFFER_TRACE(new_bh, "get write access");
  2084. ext3_journal_get_write_access(handle, new_bh);
  2085. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2086. if (EXT3_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2087. EXT3_FEATURE_INCOMPAT_FILETYPE))
  2088. new_de->file_type = old_de->file_type;
  2089. new_dir->i_version++;
  2090. BUFFER_TRACE(new_bh, "call ext3_journal_dirty_metadata");
  2091. ext3_journal_dirty_metadata(handle, new_bh);
  2092. brelse(new_bh);
  2093. new_bh = NULL;
  2094. }
  2095. /*
  2096. * Like most other Unix systems, set the ctime for inodes on a
  2097. * rename.
  2098. */
  2099. old_inode->i_ctime = CURRENT_TIME_SEC;
  2100. ext3_mark_inode_dirty(handle, old_inode);
  2101. /*
  2102. * ok, that's it
  2103. */
  2104. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2105. old_de->name_len != old_dentry->d_name.len ||
  2106. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2107. (retval = ext3_delete_entry(handle, old_dir,
  2108. old_de, old_bh)) == -ENOENT) {
  2109. /* old_de could have moved from under us during htree split, so
  2110. * make sure that we are deleting the right entry. We might
  2111. * also be pointing to a stale entry in the unused part of
  2112. * old_bh so just checking inum and the name isn't enough. */
  2113. struct buffer_head *old_bh2;
  2114. struct ext3_dir_entry_2 *old_de2;
  2115. old_bh2 = ext3_find_entry(old_dentry, &old_de2);
  2116. if (old_bh2) {
  2117. retval = ext3_delete_entry(handle, old_dir,
  2118. old_de2, old_bh2);
  2119. brelse(old_bh2);
  2120. }
  2121. }
  2122. if (retval) {
  2123. ext3_warning(old_dir->i_sb, "ext3_rename",
  2124. "Deleting old file (%lu), %d, error=%d",
  2125. old_dir->i_ino, old_dir->i_nlink, retval);
  2126. }
  2127. if (new_inode) {
  2128. drop_nlink(new_inode);
  2129. new_inode->i_ctime = CURRENT_TIME_SEC;
  2130. }
  2131. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
  2132. ext3_update_dx_flag(old_dir);
  2133. if (dir_bh) {
  2134. BUFFER_TRACE(dir_bh, "get_write_access");
  2135. ext3_journal_get_write_access(handle, dir_bh);
  2136. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2137. BUFFER_TRACE(dir_bh, "call ext3_journal_dirty_metadata");
  2138. ext3_journal_dirty_metadata(handle, dir_bh);
  2139. drop_nlink(old_dir);
  2140. if (new_inode) {
  2141. drop_nlink(new_inode);
  2142. } else {
  2143. inc_nlink(new_dir);
  2144. ext3_update_dx_flag(new_dir);
  2145. ext3_mark_inode_dirty(handle, new_dir);
  2146. }
  2147. }
  2148. ext3_mark_inode_dirty(handle, old_dir);
  2149. if (new_inode) {
  2150. ext3_mark_inode_dirty(handle, new_inode);
  2151. if (!new_inode->i_nlink)
  2152. ext3_orphan_add(handle, new_inode);
  2153. }
  2154. retval = 0;
  2155. end_rename:
  2156. brelse (dir_bh);
  2157. brelse (old_bh);
  2158. brelse (new_bh);
  2159. ext3_journal_stop(handle);
  2160. return retval;
  2161. }
  2162. /*
  2163. * directories can handle most operations...
  2164. */
  2165. struct inode_operations ext3_dir_inode_operations = {
  2166. .create = ext3_create,
  2167. .lookup = ext3_lookup,
  2168. .link = ext3_link,
  2169. .unlink = ext3_unlink,
  2170. .symlink = ext3_symlink,
  2171. .mkdir = ext3_mkdir,
  2172. .rmdir = ext3_rmdir,
  2173. .mknod = ext3_mknod,
  2174. .rename = ext3_rename,
  2175. .setattr = ext3_setattr,
  2176. #ifdef CONFIG_EXT3_FS_XATTR
  2177. .setxattr = generic_setxattr,
  2178. .getxattr = generic_getxattr,
  2179. .listxattr = ext3_listxattr,
  2180. .removexattr = generic_removexattr,
  2181. #endif
  2182. .permission = ext3_permission,
  2183. };
  2184. struct inode_operations ext3_special_inode_operations = {
  2185. .setattr = ext3_setattr,
  2186. #ifdef CONFIG_EXT3_FS_XATTR
  2187. .setxattr = generic_setxattr,
  2188. .getxattr = generic_getxattr,
  2189. .listxattr = ext3_listxattr,
  2190. .removexattr = generic_removexattr,
  2191. #endif
  2192. .permission = ext3_permission,
  2193. };