namei.c 68 KB

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