namei.c 67 KB

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