namei.c 67 KB

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