namei.c 64 KB

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