namei.c 67 KB

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