namei.c 67 KB

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