namei.c 68 KB

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