namei.c 66 KB

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