namei.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  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. return 0;
  1561. }
  1562. drop_nlink(inode);
  1563. iput(inode);
  1564. return err;
  1565. }
  1566. /*
  1567. * By the time this is called, we already have created
  1568. * the directory cache entry for the new file, but it
  1569. * is so far negative - it has no inode.
  1570. *
  1571. * If the create succeeds, we fill in the inode information
  1572. * with d_instantiate().
  1573. */
  1574. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1575. struct nameidata *nd)
  1576. {
  1577. handle_t *handle;
  1578. struct inode *inode;
  1579. int err, retries = 0;
  1580. retry:
  1581. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1582. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1583. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1584. if (IS_ERR(handle))
  1585. return PTR_ERR(handle);
  1586. if (IS_DIRSYNC(dir))
  1587. handle->h_sync = 1;
  1588. inode = ext4_new_inode (handle, dir, mode);
  1589. err = PTR_ERR(inode);
  1590. if (!IS_ERR(inode)) {
  1591. inode->i_op = &ext4_file_inode_operations;
  1592. inode->i_fop = &ext4_file_operations;
  1593. ext4_set_aops(inode);
  1594. err = ext4_add_nondir(handle, dentry, inode);
  1595. }
  1596. ext4_journal_stop(handle);
  1597. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1598. goto retry;
  1599. return err;
  1600. }
  1601. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1602. int mode, dev_t rdev)
  1603. {
  1604. handle_t *handle;
  1605. struct inode *inode;
  1606. int err, retries = 0;
  1607. if (!new_valid_dev(rdev))
  1608. return -EINVAL;
  1609. retry:
  1610. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1611. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1612. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1613. if (IS_ERR(handle))
  1614. return PTR_ERR(handle);
  1615. if (IS_DIRSYNC(dir))
  1616. handle->h_sync = 1;
  1617. inode = ext4_new_inode(handle, dir, mode);
  1618. err = PTR_ERR(inode);
  1619. if (!IS_ERR(inode)) {
  1620. init_special_inode(inode, inode->i_mode, rdev);
  1621. #ifdef CONFIG_EXT4_FS_XATTR
  1622. inode->i_op = &ext4_special_inode_operations;
  1623. #endif
  1624. err = ext4_add_nondir(handle, dentry, inode);
  1625. }
  1626. ext4_journal_stop(handle);
  1627. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1628. goto retry;
  1629. return err;
  1630. }
  1631. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1632. {
  1633. handle_t *handle;
  1634. struct inode *inode;
  1635. struct buffer_head *dir_block;
  1636. struct ext4_dir_entry_2 *de;
  1637. int err, retries = 0;
  1638. if (EXT4_DIR_LINK_MAX(dir))
  1639. return -EMLINK;
  1640. retry:
  1641. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1642. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1643. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1644. if (IS_ERR(handle))
  1645. return PTR_ERR(handle);
  1646. if (IS_DIRSYNC(dir))
  1647. handle->h_sync = 1;
  1648. inode = ext4_new_inode(handle, dir, S_IFDIR | mode);
  1649. err = PTR_ERR(inode);
  1650. if (IS_ERR(inode))
  1651. goto out_stop;
  1652. inode->i_op = &ext4_dir_inode_operations;
  1653. inode->i_fop = &ext4_dir_operations;
  1654. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1655. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1656. if (!dir_block)
  1657. goto out_clear_inode;
  1658. BUFFER_TRACE(dir_block, "get_write_access");
  1659. ext4_journal_get_write_access(handle, dir_block);
  1660. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1661. de->inode = cpu_to_le32(inode->i_ino);
  1662. de->name_len = 1;
  1663. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
  1664. strcpy(de->name, ".");
  1665. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1666. de = ext4_next_entry(de);
  1667. de->inode = cpu_to_le32(dir->i_ino);
  1668. de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
  1669. EXT4_DIR_REC_LEN(1));
  1670. de->name_len = 2;
  1671. strcpy(de->name, "..");
  1672. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1673. inode->i_nlink = 2;
  1674. BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
  1675. ext4_journal_dirty_metadata(handle, dir_block);
  1676. brelse(dir_block);
  1677. ext4_mark_inode_dirty(handle, inode);
  1678. err = ext4_add_entry(handle, dentry, inode);
  1679. if (err) {
  1680. out_clear_inode:
  1681. clear_nlink(inode);
  1682. ext4_mark_inode_dirty(handle, inode);
  1683. iput(inode);
  1684. goto out_stop;
  1685. }
  1686. ext4_inc_count(handle, dir);
  1687. ext4_update_dx_flag(dir);
  1688. ext4_mark_inode_dirty(handle, dir);
  1689. d_instantiate(dentry, inode);
  1690. out_stop:
  1691. ext4_journal_stop(handle);
  1692. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1693. goto retry;
  1694. return err;
  1695. }
  1696. /*
  1697. * routine to check that the specified directory is empty (for rmdir)
  1698. */
  1699. static int empty_dir(struct inode *inode)
  1700. {
  1701. unsigned long offset;
  1702. struct buffer_head *bh;
  1703. struct ext4_dir_entry_2 *de, *de1;
  1704. struct super_block *sb;
  1705. int err = 0;
  1706. sb = inode->i_sb;
  1707. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1708. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1709. if (err)
  1710. ext4_error(inode->i_sb, __func__,
  1711. "error %d reading directory #%lu offset 0",
  1712. err, inode->i_ino);
  1713. else
  1714. ext4_warning(inode->i_sb, __func__,
  1715. "bad directory (dir #%lu) - no data block",
  1716. inode->i_ino);
  1717. return 1;
  1718. }
  1719. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1720. de1 = ext4_next_entry(de);
  1721. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1722. !le32_to_cpu(de1->inode) ||
  1723. strcmp(".", de->name) ||
  1724. strcmp("..", de1->name)) {
  1725. ext4_warning(inode->i_sb, "empty_dir",
  1726. "bad directory (dir #%lu) - no `.' or `..'",
  1727. inode->i_ino);
  1728. brelse(bh);
  1729. return 1;
  1730. }
  1731. offset = ext4_rec_len_from_disk(de->rec_len) +
  1732. ext4_rec_len_from_disk(de1->rec_len);
  1733. de = ext4_next_entry(de1);
  1734. while (offset < inode->i_size) {
  1735. if (!bh ||
  1736. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1737. err = 0;
  1738. brelse(bh);
  1739. bh = ext4_bread(NULL, inode,
  1740. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1741. if (!bh) {
  1742. if (err)
  1743. ext4_error(sb, __func__,
  1744. "error %d reading directory"
  1745. " #%lu offset %lu",
  1746. err, inode->i_ino, offset);
  1747. offset += sb->s_blocksize;
  1748. continue;
  1749. }
  1750. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1751. }
  1752. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1753. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1754. sb->s_blocksize);
  1755. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1756. continue;
  1757. }
  1758. if (le32_to_cpu(de->inode)) {
  1759. brelse(bh);
  1760. return 0;
  1761. }
  1762. offset += ext4_rec_len_from_disk(de->rec_len);
  1763. de = ext4_next_entry(de);
  1764. }
  1765. brelse(bh);
  1766. return 1;
  1767. }
  1768. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1769. * such inodes, starting at the superblock, in case we crash before the
  1770. * file is closed/deleted, or in case the inode truncate spans multiple
  1771. * transactions and the last transaction is not recovered after a crash.
  1772. *
  1773. * At filesystem recovery time, we walk this list deleting unlinked
  1774. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1775. */
  1776. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1777. {
  1778. struct super_block *sb = inode->i_sb;
  1779. struct ext4_iloc iloc;
  1780. int err = 0, rc;
  1781. lock_super(sb);
  1782. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1783. goto out_unlock;
  1784. /* Orphan handling is only valid for files with data blocks
  1785. * being truncated, or files being unlinked. */
  1786. /* @@@ FIXME: Observation from aviro:
  1787. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1788. * here (on lock_super()), so race with ext4_link() which might bump
  1789. * ->i_nlink. For, say it, character device. Not a regular file,
  1790. * not a directory, not a symlink and ->i_nlink > 0.
  1791. */
  1792. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1793. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1794. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1795. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1796. if (err)
  1797. goto out_unlock;
  1798. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1799. if (err)
  1800. goto out_unlock;
  1801. /* Insert this inode at the head of the on-disk orphan list... */
  1802. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1803. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1804. err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  1805. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1806. if (!err)
  1807. err = rc;
  1808. /* Only add to the head of the in-memory list if all the
  1809. * previous operations succeeded. If the orphan_add is going to
  1810. * fail (possibly taking the journal offline), we can't risk
  1811. * leaving the inode on the orphan list: stray orphan-list
  1812. * entries can cause panics at unmount time.
  1813. *
  1814. * This is safe: on error we're going to ignore the orphan list
  1815. * anyway on the next recovery. */
  1816. if (!err)
  1817. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1818. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1819. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1820. inode->i_ino, NEXT_ORPHAN(inode));
  1821. out_unlock:
  1822. unlock_super(sb);
  1823. ext4_std_error(inode->i_sb, err);
  1824. return err;
  1825. }
  1826. /*
  1827. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1828. * of such inodes stored on disk, because it is finally being cleaned up.
  1829. */
  1830. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1831. {
  1832. struct list_head *prev;
  1833. struct ext4_inode_info *ei = EXT4_I(inode);
  1834. struct ext4_sb_info *sbi;
  1835. unsigned long ino_next;
  1836. struct ext4_iloc iloc;
  1837. int err = 0;
  1838. lock_super(inode->i_sb);
  1839. if (list_empty(&ei->i_orphan)) {
  1840. unlock_super(inode->i_sb);
  1841. return 0;
  1842. }
  1843. ino_next = NEXT_ORPHAN(inode);
  1844. prev = ei->i_orphan.prev;
  1845. sbi = EXT4_SB(inode->i_sb);
  1846. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1847. list_del_init(&ei->i_orphan);
  1848. /* If we're on an error path, we may not have a valid
  1849. * transaction handle with which to update the orphan list on
  1850. * disk, but we still need to remove the inode from the linked
  1851. * list in memory. */
  1852. if (!handle)
  1853. goto out;
  1854. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1855. if (err)
  1856. goto out_err;
  1857. if (prev == &sbi->s_orphan) {
  1858. jbd_debug(4, "superblock will point to %lu\n", ino_next);
  1859. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1860. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1861. if (err)
  1862. goto out_brelse;
  1863. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1864. err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
  1865. } else {
  1866. struct ext4_iloc iloc2;
  1867. struct inode *i_prev =
  1868. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1869. jbd_debug(4, "orphan inode %lu will point to %lu\n",
  1870. i_prev->i_ino, ino_next);
  1871. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1872. if (err)
  1873. goto out_brelse;
  1874. NEXT_ORPHAN(i_prev) = ino_next;
  1875. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1876. }
  1877. if (err)
  1878. goto out_brelse;
  1879. NEXT_ORPHAN(inode) = 0;
  1880. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1881. out_err:
  1882. ext4_std_error(inode->i_sb, err);
  1883. out:
  1884. unlock_super(inode->i_sb);
  1885. return err;
  1886. out_brelse:
  1887. brelse(iloc.bh);
  1888. goto out_err;
  1889. }
  1890. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1891. {
  1892. int retval;
  1893. struct inode *inode;
  1894. struct buffer_head *bh;
  1895. struct ext4_dir_entry_2 *de;
  1896. handle_t *handle;
  1897. /* Initialize quotas before so that eventual writes go in
  1898. * separate transaction */
  1899. DQUOT_INIT(dentry->d_inode);
  1900. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1901. if (IS_ERR(handle))
  1902. return PTR_ERR(handle);
  1903. retval = -ENOENT;
  1904. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1905. if (!bh)
  1906. goto end_rmdir;
  1907. if (IS_DIRSYNC(dir))
  1908. handle->h_sync = 1;
  1909. inode = dentry->d_inode;
  1910. retval = -EIO;
  1911. if (le32_to_cpu(de->inode) != inode->i_ino)
  1912. goto end_rmdir;
  1913. retval = -ENOTEMPTY;
  1914. if (!empty_dir(inode))
  1915. goto end_rmdir;
  1916. retval = ext4_delete_entry(handle, dir, de, bh);
  1917. if (retval)
  1918. goto end_rmdir;
  1919. if (!EXT4_DIR_LINK_EMPTY(inode))
  1920. ext4_warning(inode->i_sb, "ext4_rmdir",
  1921. "empty directory has too many links (%d)",
  1922. inode->i_nlink);
  1923. inode->i_version++;
  1924. clear_nlink(inode);
  1925. /* There's no need to set i_disksize: the fact that i_nlink is
  1926. * zero will ensure that the right thing happens during any
  1927. * recovery. */
  1928. inode->i_size = 0;
  1929. ext4_orphan_add(handle, inode);
  1930. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1931. ext4_mark_inode_dirty(handle, inode);
  1932. ext4_dec_count(handle, dir);
  1933. ext4_update_dx_flag(dir);
  1934. ext4_mark_inode_dirty(handle, dir);
  1935. end_rmdir:
  1936. ext4_journal_stop(handle);
  1937. brelse(bh);
  1938. return retval;
  1939. }
  1940. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  1941. {
  1942. int retval;
  1943. struct inode *inode;
  1944. struct buffer_head *bh;
  1945. struct ext4_dir_entry_2 *de;
  1946. handle_t *handle;
  1947. /* Initialize quotas before so that eventual writes go
  1948. * in separate transaction */
  1949. DQUOT_INIT(dentry->d_inode);
  1950. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1951. if (IS_ERR(handle))
  1952. return PTR_ERR(handle);
  1953. if (IS_DIRSYNC(dir))
  1954. handle->h_sync = 1;
  1955. retval = -ENOENT;
  1956. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1957. if (!bh)
  1958. goto end_unlink;
  1959. inode = dentry->d_inode;
  1960. retval = -EIO;
  1961. if (le32_to_cpu(de->inode) != inode->i_ino)
  1962. goto end_unlink;
  1963. if (!inode->i_nlink) {
  1964. ext4_warning(inode->i_sb, "ext4_unlink",
  1965. "Deleting nonexistent file (%lu), %d",
  1966. inode->i_ino, inode->i_nlink);
  1967. inode->i_nlink = 1;
  1968. }
  1969. retval = ext4_delete_entry(handle, dir, de, bh);
  1970. if (retval)
  1971. goto end_unlink;
  1972. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  1973. ext4_update_dx_flag(dir);
  1974. ext4_mark_inode_dirty(handle, dir);
  1975. drop_nlink(inode);
  1976. if (!inode->i_nlink)
  1977. ext4_orphan_add(handle, inode);
  1978. inode->i_ctime = ext4_current_time(inode);
  1979. ext4_mark_inode_dirty(handle, inode);
  1980. retval = 0;
  1981. end_unlink:
  1982. ext4_journal_stop(handle);
  1983. brelse(bh);
  1984. return retval;
  1985. }
  1986. static int ext4_symlink(struct inode *dir,
  1987. struct dentry *dentry, const char *symname)
  1988. {
  1989. handle_t *handle;
  1990. struct inode *inode;
  1991. int l, err, retries = 0;
  1992. l = strlen(symname)+1;
  1993. if (l > dir->i_sb->s_blocksize)
  1994. return -ENAMETOOLONG;
  1995. retry:
  1996. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1997. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  1998. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1999. if (IS_ERR(handle))
  2000. return PTR_ERR(handle);
  2001. if (IS_DIRSYNC(dir))
  2002. handle->h_sync = 1;
  2003. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO);
  2004. err = PTR_ERR(inode);
  2005. if (IS_ERR(inode))
  2006. goto out_stop;
  2007. if (l > sizeof(EXT4_I(inode)->i_data)) {
  2008. inode->i_op = &ext4_symlink_inode_operations;
  2009. ext4_set_aops(inode);
  2010. /*
  2011. * page_symlink() calls into ext4_prepare/commit_write.
  2012. * We have a transaction open. All is sweetness. It also sets
  2013. * i_size in generic_commit_write().
  2014. */
  2015. err = __page_symlink(inode, symname, l,
  2016. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  2017. if (err) {
  2018. clear_nlink(inode);
  2019. ext4_mark_inode_dirty(handle, inode);
  2020. iput(inode);
  2021. goto out_stop;
  2022. }
  2023. } else {
  2024. /* clear the extent format for fast symlink */
  2025. EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
  2026. inode->i_op = &ext4_fast_symlink_inode_operations;
  2027. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2028. inode->i_size = l-1;
  2029. }
  2030. EXT4_I(inode)->i_disksize = inode->i_size;
  2031. err = ext4_add_nondir(handle, dentry, inode);
  2032. out_stop:
  2033. ext4_journal_stop(handle);
  2034. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2035. goto retry;
  2036. return err;
  2037. }
  2038. static int ext4_link(struct dentry *old_dentry,
  2039. struct inode *dir, struct dentry *dentry)
  2040. {
  2041. handle_t *handle;
  2042. struct inode *inode = old_dentry->d_inode;
  2043. int err, retries = 0;
  2044. if (EXT4_DIR_LINK_MAX(inode))
  2045. return -EMLINK;
  2046. /*
  2047. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  2048. * otherwise has the potential to corrupt the orphan inode list.
  2049. */
  2050. if (inode->i_nlink == 0)
  2051. return -ENOENT;
  2052. retry:
  2053. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2054. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2055. if (IS_ERR(handle))
  2056. return PTR_ERR(handle);
  2057. if (IS_DIRSYNC(dir))
  2058. handle->h_sync = 1;
  2059. inode->i_ctime = ext4_current_time(inode);
  2060. ext4_inc_count(handle, inode);
  2061. atomic_inc(&inode->i_count);
  2062. err = ext4_add_nondir(handle, dentry, inode);
  2063. ext4_journal_stop(handle);
  2064. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2065. goto retry;
  2066. return err;
  2067. }
  2068. #define PARENT_INO(buffer) \
  2069. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
  2070. /*
  2071. * Anybody can rename anything with this: the permission checks are left to the
  2072. * higher-level routines.
  2073. */
  2074. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2075. struct inode *new_dir, struct dentry *new_dentry)
  2076. {
  2077. handle_t *handle;
  2078. struct inode *old_inode, *new_inode;
  2079. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2080. struct ext4_dir_entry_2 *old_de, *new_de;
  2081. int retval;
  2082. old_bh = new_bh = dir_bh = NULL;
  2083. /* Initialize quotas before so that eventual writes go
  2084. * in separate transaction */
  2085. if (new_dentry->d_inode)
  2086. DQUOT_INIT(new_dentry->d_inode);
  2087. handle = ext4_journal_start(old_dir, 2 *
  2088. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2089. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2090. if (IS_ERR(handle))
  2091. return PTR_ERR(handle);
  2092. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2093. handle->h_sync = 1;
  2094. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2095. /*
  2096. * Check for inode number is _not_ due to possible IO errors.
  2097. * We might rmdir the source, keep it as pwd of some process
  2098. * and merrily kill the link to whatever was created under the
  2099. * same name. Goodbye sticky bit ;-<
  2100. */
  2101. old_inode = old_dentry->d_inode;
  2102. retval = -ENOENT;
  2103. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2104. goto end_rename;
  2105. new_inode = new_dentry->d_inode;
  2106. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2107. if (new_bh) {
  2108. if (!new_inode) {
  2109. brelse(new_bh);
  2110. new_bh = NULL;
  2111. }
  2112. }
  2113. if (S_ISDIR(old_inode->i_mode)) {
  2114. if (new_inode) {
  2115. retval = -ENOTEMPTY;
  2116. if (!empty_dir(new_inode))
  2117. goto end_rename;
  2118. }
  2119. retval = -EIO;
  2120. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2121. if (!dir_bh)
  2122. goto end_rename;
  2123. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2124. goto end_rename;
  2125. retval = -EMLINK;
  2126. if (!new_inode && new_dir != old_dir &&
  2127. new_dir->i_nlink >= EXT4_LINK_MAX)
  2128. goto end_rename;
  2129. }
  2130. if (!new_bh) {
  2131. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2132. if (retval)
  2133. goto end_rename;
  2134. } else {
  2135. BUFFER_TRACE(new_bh, "get write access");
  2136. ext4_journal_get_write_access(handle, new_bh);
  2137. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2138. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2139. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2140. new_de->file_type = old_de->file_type;
  2141. new_dir->i_version++;
  2142. new_dir->i_ctime = new_dir->i_mtime =
  2143. ext4_current_time(new_dir);
  2144. ext4_mark_inode_dirty(handle, new_dir);
  2145. BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
  2146. ext4_journal_dirty_metadata(handle, new_bh);
  2147. brelse(new_bh);
  2148. new_bh = NULL;
  2149. }
  2150. /*
  2151. * Like most other Unix systems, set the ctime for inodes on a
  2152. * rename.
  2153. */
  2154. old_inode->i_ctime = ext4_current_time(old_inode);
  2155. ext4_mark_inode_dirty(handle, old_inode);
  2156. /*
  2157. * ok, that's it
  2158. */
  2159. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2160. old_de->name_len != old_dentry->d_name.len ||
  2161. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2162. (retval = ext4_delete_entry(handle, old_dir,
  2163. old_de, old_bh)) == -ENOENT) {
  2164. /* old_de could have moved from under us during htree split, so
  2165. * make sure that we are deleting the right entry. We might
  2166. * also be pointing to a stale entry in the unused part of
  2167. * old_bh so just checking inum and the name isn't enough. */
  2168. struct buffer_head *old_bh2;
  2169. struct ext4_dir_entry_2 *old_de2;
  2170. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2171. if (old_bh2) {
  2172. retval = ext4_delete_entry(handle, old_dir,
  2173. old_de2, old_bh2);
  2174. brelse(old_bh2);
  2175. }
  2176. }
  2177. if (retval) {
  2178. ext4_warning(old_dir->i_sb, "ext4_rename",
  2179. "Deleting old file (%lu), %d, error=%d",
  2180. old_dir->i_ino, old_dir->i_nlink, retval);
  2181. }
  2182. if (new_inode) {
  2183. ext4_dec_count(handle, new_inode);
  2184. new_inode->i_ctime = ext4_current_time(new_inode);
  2185. }
  2186. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2187. ext4_update_dx_flag(old_dir);
  2188. if (dir_bh) {
  2189. BUFFER_TRACE(dir_bh, "get_write_access");
  2190. ext4_journal_get_write_access(handle, dir_bh);
  2191. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2192. BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
  2193. ext4_journal_dirty_metadata(handle, dir_bh);
  2194. ext4_dec_count(handle, old_dir);
  2195. if (new_inode) {
  2196. /* checked empty_dir above, can't have another parent,
  2197. * ext4_dec_count() won't work for many-linked dirs */
  2198. new_inode->i_nlink = 0;
  2199. } else {
  2200. ext4_inc_count(handle, new_dir);
  2201. ext4_update_dx_flag(new_dir);
  2202. ext4_mark_inode_dirty(handle, new_dir);
  2203. }
  2204. }
  2205. ext4_mark_inode_dirty(handle, old_dir);
  2206. if (new_inode) {
  2207. ext4_mark_inode_dirty(handle, new_inode);
  2208. if (!new_inode->i_nlink)
  2209. ext4_orphan_add(handle, new_inode);
  2210. }
  2211. retval = 0;
  2212. end_rename:
  2213. brelse(dir_bh);
  2214. brelse(old_bh);
  2215. brelse(new_bh);
  2216. ext4_journal_stop(handle);
  2217. return retval;
  2218. }
  2219. /*
  2220. * directories can handle most operations...
  2221. */
  2222. const struct inode_operations ext4_dir_inode_operations = {
  2223. .create = ext4_create,
  2224. .lookup = ext4_lookup,
  2225. .link = ext4_link,
  2226. .unlink = ext4_unlink,
  2227. .symlink = ext4_symlink,
  2228. .mkdir = ext4_mkdir,
  2229. .rmdir = ext4_rmdir,
  2230. .mknod = ext4_mknod,
  2231. .rename = ext4_rename,
  2232. .setattr = ext4_setattr,
  2233. #ifdef CONFIG_EXT4_FS_XATTR
  2234. .setxattr = generic_setxattr,
  2235. .getxattr = generic_getxattr,
  2236. .listxattr = ext4_listxattr,
  2237. .removexattr = generic_removexattr,
  2238. #endif
  2239. .permission = ext4_permission,
  2240. };
  2241. const struct inode_operations ext4_special_inode_operations = {
  2242. .setattr = ext4_setattr,
  2243. #ifdef CONFIG_EXT4_FS_XATTR
  2244. .setxattr = generic_setxattr,
  2245. .getxattr = generic_getxattr,
  2246. .listxattr = ext4_listxattr,
  2247. .removexattr = generic_removexattr,
  2248. #endif
  2249. .permission = ext4_permission,
  2250. };