namei.c 67 KB

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