namei.c 68 KB

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