namei.c 65 KB

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