namei.c 66 KB

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