namei.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/time.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/bio.h>
  36. #include "ext4.h"
  37. #include "ext4_jbd2.h"
  38. #include "xattr.h"
  39. #include "acl.h"
  40. #include <trace/events/ext4.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. static struct buffer_head *ext4_append(handle_t *handle,
  48. struct inode *inode,
  49. ext4_lblk_t *block)
  50. {
  51. struct buffer_head *bh;
  52. int err = 0;
  53. if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
  54. ((inode->i_size >> 10) >=
  55. EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
  56. return ERR_PTR(-ENOSPC);
  57. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  58. bh = ext4_bread(handle, inode, *block, 1, &err);
  59. if (!bh)
  60. return ERR_PTR(err);
  61. inode->i_size += inode->i_sb->s_blocksize;
  62. EXT4_I(inode)->i_disksize = inode->i_size;
  63. err = ext4_journal_get_write_access(handle, bh);
  64. if (err) {
  65. brelse(bh);
  66. ext4_std_error(inode->i_sb, err);
  67. return ERR_PTR(err);
  68. }
  69. return bh;
  70. }
  71. static int ext4_dx_csum_verify(struct inode *inode,
  72. struct ext4_dir_entry *dirent);
  73. typedef enum {
  74. EITHER, INDEX, DIRENT
  75. } dirblock_type_t;
  76. #define ext4_read_dirblock(inode, block, type) \
  77. __ext4_read_dirblock((inode), (block), (type), __LINE__)
  78. static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
  79. ext4_lblk_t block,
  80. dirblock_type_t type,
  81. unsigned int line)
  82. {
  83. struct buffer_head *bh;
  84. struct ext4_dir_entry *dirent;
  85. int err = 0, is_dx_block = 0;
  86. bh = ext4_bread(NULL, inode, block, 0, &err);
  87. if (!bh) {
  88. if (err == 0) {
  89. ext4_error_inode(inode, __func__, line, block,
  90. "Directory hole found");
  91. return ERR_PTR(-EIO);
  92. }
  93. __ext4_warning(inode->i_sb, __func__, line,
  94. "error reading directory block "
  95. "(ino %lu, block %lu)", inode->i_ino,
  96. (unsigned long) block);
  97. return ERR_PTR(err);
  98. }
  99. dirent = (struct ext4_dir_entry *) bh->b_data;
  100. /* Determine whether or not we have an index block */
  101. if (is_dx(inode)) {
  102. if (block == 0)
  103. is_dx_block = 1;
  104. else if (ext4_rec_len_from_disk(dirent->rec_len,
  105. inode->i_sb->s_blocksize) ==
  106. inode->i_sb->s_blocksize)
  107. is_dx_block = 1;
  108. }
  109. if (!is_dx_block && type == INDEX) {
  110. ext4_error_inode(inode, __func__, line, block,
  111. "directory leaf block found instead of index block");
  112. return ERR_PTR(-EIO);
  113. }
  114. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  115. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) ||
  116. buffer_verified(bh))
  117. return bh;
  118. /*
  119. * An empty leaf block can get mistaken for a index block; for
  120. * this reason, we can only check the index checksum when the
  121. * caller is sure it should be an index block.
  122. */
  123. if (is_dx_block && type == INDEX) {
  124. if (ext4_dx_csum_verify(inode, dirent))
  125. set_buffer_verified(bh);
  126. else {
  127. ext4_error_inode(inode, __func__, line, block,
  128. "Directory index failed checksum");
  129. brelse(bh);
  130. return ERR_PTR(-EIO);
  131. }
  132. }
  133. if (!is_dx_block) {
  134. if (ext4_dirent_csum_verify(inode, dirent))
  135. set_buffer_verified(bh);
  136. else {
  137. ext4_error_inode(inode, __func__, line, block,
  138. "Directory block failed checksum");
  139. brelse(bh);
  140. return ERR_PTR(-EIO);
  141. }
  142. }
  143. return bh;
  144. }
  145. #ifndef assert
  146. #define assert(test) J_ASSERT(test)
  147. #endif
  148. #ifdef DX_DEBUG
  149. #define dxtrace(command) command
  150. #else
  151. #define dxtrace(command)
  152. #endif
  153. struct fake_dirent
  154. {
  155. __le32 inode;
  156. __le16 rec_len;
  157. u8 name_len;
  158. u8 file_type;
  159. };
  160. struct dx_countlimit
  161. {
  162. __le16 limit;
  163. __le16 count;
  164. };
  165. struct dx_entry
  166. {
  167. __le32 hash;
  168. __le32 block;
  169. };
  170. /*
  171. * dx_root_info is laid out so that if it should somehow get overlaid by a
  172. * dirent the two low bits of the hash version will be zero. Therefore, the
  173. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  174. */
  175. struct dx_root
  176. {
  177. struct fake_dirent dot;
  178. char dot_name[4];
  179. struct fake_dirent dotdot;
  180. char dotdot_name[4];
  181. struct dx_root_info
  182. {
  183. __le32 reserved_zero;
  184. u8 hash_version;
  185. u8 info_length; /* 8 */
  186. u8 indirect_levels;
  187. u8 unused_flags;
  188. }
  189. info;
  190. struct dx_entry entries[0];
  191. };
  192. struct dx_node
  193. {
  194. struct fake_dirent fake;
  195. struct dx_entry entries[0];
  196. };
  197. struct dx_frame
  198. {
  199. struct buffer_head *bh;
  200. struct dx_entry *entries;
  201. struct dx_entry *at;
  202. };
  203. struct dx_map_entry
  204. {
  205. u32 hash;
  206. u16 offs;
  207. u16 size;
  208. };
  209. /*
  210. * This goes at the end of each htree block.
  211. */
  212. struct dx_tail {
  213. u32 dt_reserved;
  214. __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
  215. };
  216. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  217. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  218. static inline unsigned dx_get_hash(struct dx_entry *entry);
  219. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  220. static unsigned dx_get_count(struct dx_entry *entries);
  221. static unsigned dx_get_limit(struct dx_entry *entries);
  222. static void dx_set_count(struct dx_entry *entries, unsigned value);
  223. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  224. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  225. static unsigned dx_node_limit(struct inode *dir);
  226. static struct dx_frame *dx_probe(const struct qstr *d_name,
  227. struct inode *dir,
  228. struct dx_hash_info *hinfo,
  229. struct dx_frame *frame,
  230. int *err);
  231. static void dx_release(struct dx_frame *frames);
  232. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  233. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  234. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  235. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  236. struct dx_map_entry *offsets, int count, unsigned blocksize);
  237. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  238. static void dx_insert_block(struct dx_frame *frame,
  239. u32 hash, ext4_lblk_t block);
  240. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  241. struct dx_frame *frame,
  242. struct dx_frame *frames,
  243. __u32 *start_hash);
  244. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  245. const struct qstr *d_name,
  246. struct ext4_dir_entry_2 **res_dir,
  247. int *err);
  248. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  249. struct inode *inode);
  250. /* checksumming functions */
  251. void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
  252. unsigned int blocksize)
  253. {
  254. memset(t, 0, sizeof(struct ext4_dir_entry_tail));
  255. t->det_rec_len = ext4_rec_len_to_disk(
  256. sizeof(struct ext4_dir_entry_tail), blocksize);
  257. t->det_reserved_ft = EXT4_FT_DIR_CSUM;
  258. }
  259. /* Walk through a dirent block to find a checksum "dirent" at the tail */
  260. static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
  261. struct ext4_dir_entry *de)
  262. {
  263. struct ext4_dir_entry_tail *t;
  264. #ifdef PARANOID
  265. struct ext4_dir_entry *d, *top;
  266. d = de;
  267. top = (struct ext4_dir_entry *)(((void *)de) +
  268. (EXT4_BLOCK_SIZE(inode->i_sb) -
  269. sizeof(struct ext4_dir_entry_tail)));
  270. while (d < top && d->rec_len)
  271. d = (struct ext4_dir_entry *)(((void *)d) +
  272. le16_to_cpu(d->rec_len));
  273. if (d != top)
  274. return NULL;
  275. t = (struct ext4_dir_entry_tail *)d;
  276. #else
  277. t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
  278. #endif
  279. if (t->det_reserved_zero1 ||
  280. le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
  281. t->det_reserved_zero2 ||
  282. t->det_reserved_ft != EXT4_FT_DIR_CSUM)
  283. return NULL;
  284. return t;
  285. }
  286. static __le32 ext4_dirent_csum(struct inode *inode,
  287. struct ext4_dir_entry *dirent, int size)
  288. {
  289. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  290. struct ext4_inode_info *ei = EXT4_I(inode);
  291. __u32 csum;
  292. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  293. return cpu_to_le32(csum);
  294. }
  295. static void warn_no_space_for_csum(struct inode *inode)
  296. {
  297. ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
  298. "checksum. Please run e2fsck -D.", inode->i_ino);
  299. }
  300. int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
  301. {
  302. struct ext4_dir_entry_tail *t;
  303. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  304. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  305. return 1;
  306. t = get_dirent_tail(inode, dirent);
  307. if (!t) {
  308. warn_no_space_for_csum(inode);
  309. return 0;
  310. }
  311. if (t->det_checksum != ext4_dirent_csum(inode, dirent,
  312. (void *)t - (void *)dirent))
  313. return 0;
  314. return 1;
  315. }
  316. static void ext4_dirent_csum_set(struct inode *inode,
  317. struct ext4_dir_entry *dirent)
  318. {
  319. struct ext4_dir_entry_tail *t;
  320. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  321. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  322. return;
  323. t = get_dirent_tail(inode, dirent);
  324. if (!t) {
  325. warn_no_space_for_csum(inode);
  326. return;
  327. }
  328. t->det_checksum = ext4_dirent_csum(inode, dirent,
  329. (void *)t - (void *)dirent);
  330. }
  331. int ext4_handle_dirty_dirent_node(handle_t *handle,
  332. struct inode *inode,
  333. struct buffer_head *bh)
  334. {
  335. ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  336. return ext4_handle_dirty_metadata(handle, inode, bh);
  337. }
  338. static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
  339. struct ext4_dir_entry *dirent,
  340. int *offset)
  341. {
  342. struct ext4_dir_entry *dp;
  343. struct dx_root_info *root;
  344. int count_offset;
  345. if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
  346. count_offset = 8;
  347. else if (le16_to_cpu(dirent->rec_len) == 12) {
  348. dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
  349. if (le16_to_cpu(dp->rec_len) !=
  350. EXT4_BLOCK_SIZE(inode->i_sb) - 12)
  351. return NULL;
  352. root = (struct dx_root_info *)(((void *)dp + 12));
  353. if (root->reserved_zero ||
  354. root->info_length != sizeof(struct dx_root_info))
  355. return NULL;
  356. count_offset = 32;
  357. } else
  358. return NULL;
  359. if (offset)
  360. *offset = count_offset;
  361. return (struct dx_countlimit *)(((void *)dirent) + count_offset);
  362. }
  363. static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
  364. int count_offset, int count, struct dx_tail *t)
  365. {
  366. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  367. struct ext4_inode_info *ei = EXT4_I(inode);
  368. __u32 csum;
  369. __le32 save_csum;
  370. int size;
  371. size = count_offset + (count * sizeof(struct dx_entry));
  372. save_csum = t->dt_checksum;
  373. t->dt_checksum = 0;
  374. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  375. csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
  376. t->dt_checksum = save_csum;
  377. return cpu_to_le32(csum);
  378. }
  379. static int ext4_dx_csum_verify(struct inode *inode,
  380. struct ext4_dir_entry *dirent)
  381. {
  382. struct dx_countlimit *c;
  383. struct dx_tail *t;
  384. int count_offset, limit, count;
  385. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  386. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  387. return 1;
  388. c = get_dx_countlimit(inode, dirent, &count_offset);
  389. if (!c) {
  390. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  391. return 1;
  392. }
  393. limit = le16_to_cpu(c->limit);
  394. count = le16_to_cpu(c->count);
  395. if (count_offset + (limit * sizeof(struct dx_entry)) >
  396. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  397. warn_no_space_for_csum(inode);
  398. return 1;
  399. }
  400. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  401. if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
  402. count, t))
  403. return 0;
  404. return 1;
  405. }
  406. static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
  407. {
  408. struct dx_countlimit *c;
  409. struct dx_tail *t;
  410. int count_offset, limit, count;
  411. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  412. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  413. return;
  414. c = get_dx_countlimit(inode, dirent, &count_offset);
  415. if (!c) {
  416. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  417. return;
  418. }
  419. limit = le16_to_cpu(c->limit);
  420. count = le16_to_cpu(c->count);
  421. if (count_offset + (limit * sizeof(struct dx_entry)) >
  422. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  423. warn_no_space_for_csum(inode);
  424. return;
  425. }
  426. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  427. t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
  428. }
  429. static inline int ext4_handle_dirty_dx_node(handle_t *handle,
  430. struct inode *inode,
  431. struct buffer_head *bh)
  432. {
  433. ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  434. return ext4_handle_dirty_metadata(handle, inode, bh);
  435. }
  436. /*
  437. * p is at least 6 bytes before the end of page
  438. */
  439. static inline struct ext4_dir_entry_2 *
  440. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  441. {
  442. return (struct ext4_dir_entry_2 *)((char *)p +
  443. ext4_rec_len_from_disk(p->rec_len, blocksize));
  444. }
  445. /*
  446. * Future: use high four bits of block for coalesce-on-delete flags
  447. * Mask them off for now.
  448. */
  449. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  450. {
  451. return le32_to_cpu(entry->block) & 0x00ffffff;
  452. }
  453. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  454. {
  455. entry->block = cpu_to_le32(value);
  456. }
  457. static inline unsigned dx_get_hash(struct dx_entry *entry)
  458. {
  459. return le32_to_cpu(entry->hash);
  460. }
  461. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  462. {
  463. entry->hash = cpu_to_le32(value);
  464. }
  465. static inline unsigned dx_get_count(struct dx_entry *entries)
  466. {
  467. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  468. }
  469. static inline unsigned dx_get_limit(struct dx_entry *entries)
  470. {
  471. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  472. }
  473. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  474. {
  475. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  476. }
  477. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  478. {
  479. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  480. }
  481. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  482. {
  483. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  484. EXT4_DIR_REC_LEN(2) - infosize;
  485. if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
  486. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  487. entry_space -= sizeof(struct dx_tail);
  488. return entry_space / sizeof(struct dx_entry);
  489. }
  490. static inline unsigned dx_node_limit(struct inode *dir)
  491. {
  492. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  493. if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
  494. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  495. entry_space -= sizeof(struct dx_tail);
  496. return entry_space / sizeof(struct dx_entry);
  497. }
  498. /*
  499. * Debug
  500. */
  501. #ifdef DX_DEBUG
  502. static void dx_show_index(char * label, struct dx_entry *entries)
  503. {
  504. int i, n = dx_get_count (entries);
  505. printk(KERN_DEBUG "%s index ", label);
  506. for (i = 0; i < n; i++) {
  507. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  508. 0, (unsigned long)dx_get_block(entries + i));
  509. }
  510. printk("\n");
  511. }
  512. struct stats
  513. {
  514. unsigned names;
  515. unsigned space;
  516. unsigned bcount;
  517. };
  518. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  519. int size, int show_names)
  520. {
  521. unsigned names = 0, space = 0;
  522. char *base = (char *) de;
  523. struct dx_hash_info h = *hinfo;
  524. printk("names: ");
  525. while ((char *) de < base + size)
  526. {
  527. if (de->inode)
  528. {
  529. if (show_names)
  530. {
  531. int len = de->name_len;
  532. char *name = de->name;
  533. while (len--) printk("%c", *name++);
  534. ext4fs_dirhash(de->name, de->name_len, &h);
  535. printk(":%x.%u ", h.hash,
  536. (unsigned) ((char *) de - base));
  537. }
  538. space += EXT4_DIR_REC_LEN(de->name_len);
  539. names++;
  540. }
  541. de = ext4_next_entry(de, size);
  542. }
  543. printk("(%i)\n", names);
  544. return (struct stats) { names, space, 1 };
  545. }
  546. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  547. struct dx_entry *entries, int levels)
  548. {
  549. unsigned blocksize = dir->i_sb->s_blocksize;
  550. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  551. unsigned bcount = 0;
  552. struct buffer_head *bh;
  553. int err;
  554. printk("%i indexed blocks...\n", count);
  555. for (i = 0; i < count; i++, entries++)
  556. {
  557. ext4_lblk_t block = dx_get_block(entries);
  558. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  559. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  560. struct stats stats;
  561. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  562. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  563. stats = levels?
  564. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  565. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  566. names += stats.names;
  567. space += stats.space;
  568. bcount += stats.bcount;
  569. brelse(bh);
  570. }
  571. if (bcount)
  572. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  573. levels ? "" : " ", names, space/bcount,
  574. (space/bcount)*100/blocksize);
  575. return (struct stats) { names, space, bcount};
  576. }
  577. #endif /* DX_DEBUG */
  578. /*
  579. * Probe for a directory leaf block to search.
  580. *
  581. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  582. * error in the directory index, and the caller should fall back to
  583. * searching the directory normally. The callers of dx_probe **MUST**
  584. * check for this error code, and make sure it never gets reflected
  585. * back to userspace.
  586. */
  587. static struct dx_frame *
  588. dx_probe(const struct qstr *d_name, struct inode *dir,
  589. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  590. {
  591. unsigned count, indirect;
  592. struct dx_entry *at, *entries, *p, *q, *m;
  593. struct dx_root *root;
  594. struct buffer_head *bh;
  595. struct dx_frame *frame = frame_in;
  596. u32 hash;
  597. frame->bh = NULL;
  598. bh = ext4_read_dirblock(dir, 0, INDEX);
  599. if (IS_ERR(bh)) {
  600. *err = PTR_ERR(bh);
  601. goto fail;
  602. }
  603. root = (struct dx_root *) bh->b_data;
  604. if (root->info.hash_version != DX_HASH_TEA &&
  605. root->info.hash_version != DX_HASH_HALF_MD4 &&
  606. root->info.hash_version != DX_HASH_LEGACY) {
  607. ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
  608. root->info.hash_version);
  609. brelse(bh);
  610. *err = ERR_BAD_DX_DIR;
  611. goto fail;
  612. }
  613. hinfo->hash_version = root->info.hash_version;
  614. if (hinfo->hash_version <= DX_HASH_TEA)
  615. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  616. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  617. if (d_name)
  618. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  619. hash = hinfo->hash;
  620. if (root->info.unused_flags & 1) {
  621. ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
  622. root->info.unused_flags);
  623. brelse(bh);
  624. *err = ERR_BAD_DX_DIR;
  625. goto fail;
  626. }
  627. if ((indirect = root->info.indirect_levels) > 1) {
  628. ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
  629. root->info.indirect_levels);
  630. brelse(bh);
  631. *err = ERR_BAD_DX_DIR;
  632. goto fail;
  633. }
  634. entries = (struct dx_entry *) (((char *)&root->info) +
  635. root->info.info_length);
  636. if (dx_get_limit(entries) != dx_root_limit(dir,
  637. root->info.info_length)) {
  638. ext4_warning(dir->i_sb, "dx entry: limit != root limit");
  639. brelse(bh);
  640. *err = ERR_BAD_DX_DIR;
  641. goto fail;
  642. }
  643. dxtrace(printk("Look up %x", hash));
  644. while (1)
  645. {
  646. count = dx_get_count(entries);
  647. if (!count || count > dx_get_limit(entries)) {
  648. ext4_warning(dir->i_sb,
  649. "dx entry: no count or count > limit");
  650. brelse(bh);
  651. *err = ERR_BAD_DX_DIR;
  652. goto fail2;
  653. }
  654. p = entries + 1;
  655. q = entries + count - 1;
  656. while (p <= q)
  657. {
  658. m = p + (q - p)/2;
  659. dxtrace(printk("."));
  660. if (dx_get_hash(m) > hash)
  661. q = m - 1;
  662. else
  663. p = m + 1;
  664. }
  665. if (0) // linear search cross check
  666. {
  667. unsigned n = count - 1;
  668. at = entries;
  669. while (n--)
  670. {
  671. dxtrace(printk(","));
  672. if (dx_get_hash(++at) > hash)
  673. {
  674. at--;
  675. break;
  676. }
  677. }
  678. assert (at == p - 1);
  679. }
  680. at = p - 1;
  681. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  682. frame->bh = bh;
  683. frame->entries = entries;
  684. frame->at = at;
  685. if (!indirect--) return frame;
  686. bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
  687. if (IS_ERR(bh)) {
  688. *err = PTR_ERR(bh);
  689. goto fail2;
  690. }
  691. entries = ((struct dx_node *) bh->b_data)->entries;
  692. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  693. ext4_warning(dir->i_sb,
  694. "dx entry: limit != node limit");
  695. brelse(bh);
  696. *err = ERR_BAD_DX_DIR;
  697. goto fail2;
  698. }
  699. frame++;
  700. frame->bh = NULL;
  701. }
  702. fail2:
  703. while (frame >= frame_in) {
  704. brelse(frame->bh);
  705. frame--;
  706. }
  707. fail:
  708. if (*err == ERR_BAD_DX_DIR)
  709. ext4_warning(dir->i_sb,
  710. "Corrupt dir inode %lu, running e2fsck is "
  711. "recommended.", dir->i_ino);
  712. return NULL;
  713. }
  714. static void dx_release (struct dx_frame *frames)
  715. {
  716. if (frames[0].bh == NULL)
  717. return;
  718. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  719. brelse(frames[1].bh);
  720. brelse(frames[0].bh);
  721. }
  722. /*
  723. * This function increments the frame pointer to search the next leaf
  724. * block, and reads in the necessary intervening nodes if the search
  725. * should be necessary. Whether or not the search is necessary is
  726. * controlled by the hash parameter. If the hash value is even, then
  727. * the search is only continued if the next block starts with that
  728. * hash value. This is used if we are searching for a specific file.
  729. *
  730. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  731. *
  732. * This function returns 1 if the caller should continue to search,
  733. * or 0 if it should not. If there is an error reading one of the
  734. * index blocks, it will a negative error code.
  735. *
  736. * If start_hash is non-null, it will be filled in with the starting
  737. * hash of the next page.
  738. */
  739. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  740. struct dx_frame *frame,
  741. struct dx_frame *frames,
  742. __u32 *start_hash)
  743. {
  744. struct dx_frame *p;
  745. struct buffer_head *bh;
  746. int num_frames = 0;
  747. __u32 bhash;
  748. p = frame;
  749. /*
  750. * Find the next leaf page by incrementing the frame pointer.
  751. * If we run out of entries in the interior node, loop around and
  752. * increment pointer in the parent node. When we break out of
  753. * this loop, num_frames indicates the number of interior
  754. * nodes need to be read.
  755. */
  756. while (1) {
  757. if (++(p->at) < p->entries + dx_get_count(p->entries))
  758. break;
  759. if (p == frames)
  760. return 0;
  761. num_frames++;
  762. p--;
  763. }
  764. /*
  765. * If the hash is 1, then continue only if the next page has a
  766. * continuation hash of any value. This is used for readdir
  767. * handling. Otherwise, check to see if the hash matches the
  768. * desired contiuation hash. If it doesn't, return since
  769. * there's no point to read in the successive index pages.
  770. */
  771. bhash = dx_get_hash(p->at);
  772. if (start_hash)
  773. *start_hash = bhash;
  774. if ((hash & 1) == 0) {
  775. if ((bhash & ~1) != hash)
  776. return 0;
  777. }
  778. /*
  779. * If the hash is HASH_NB_ALWAYS, we always go to the next
  780. * block so no check is necessary
  781. */
  782. while (num_frames--) {
  783. bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
  784. if (IS_ERR(bh))
  785. return PTR_ERR(bh);
  786. p++;
  787. brelse(p->bh);
  788. p->bh = bh;
  789. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  790. }
  791. return 1;
  792. }
  793. /*
  794. * This function fills a red-black tree with information from a
  795. * directory block. It returns the number directory entries loaded
  796. * into the tree. If there is an error it is returned in err.
  797. */
  798. static int htree_dirblock_to_tree(struct file *dir_file,
  799. struct inode *dir, ext4_lblk_t block,
  800. struct dx_hash_info *hinfo,
  801. __u32 start_hash, __u32 start_minor_hash)
  802. {
  803. struct buffer_head *bh;
  804. struct ext4_dir_entry_2 *de, *top;
  805. int err = 0, count = 0;
  806. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  807. (unsigned long)block));
  808. bh = ext4_read_dirblock(dir, block, DIRENT);
  809. if (IS_ERR(bh))
  810. return PTR_ERR(bh);
  811. de = (struct ext4_dir_entry_2 *) bh->b_data;
  812. top = (struct ext4_dir_entry_2 *) ((char *) de +
  813. dir->i_sb->s_blocksize -
  814. EXT4_DIR_REC_LEN(0));
  815. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  816. if (ext4_check_dir_entry(dir, NULL, de, bh,
  817. bh->b_data, bh->b_size,
  818. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  819. + ((char *)de - bh->b_data))) {
  820. /* silently ignore the rest of the block */
  821. break;
  822. }
  823. ext4fs_dirhash(de->name, de->name_len, hinfo);
  824. if ((hinfo->hash < start_hash) ||
  825. ((hinfo->hash == start_hash) &&
  826. (hinfo->minor_hash < start_minor_hash)))
  827. continue;
  828. if (de->inode == 0)
  829. continue;
  830. if ((err = ext4_htree_store_dirent(dir_file,
  831. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  832. brelse(bh);
  833. return err;
  834. }
  835. count++;
  836. }
  837. brelse(bh);
  838. return count;
  839. }
  840. /*
  841. * This function fills a red-black tree with information from a
  842. * directory. We start scanning the directory in hash order, starting
  843. * at start_hash and start_minor_hash.
  844. *
  845. * This function returns the number of entries inserted into the tree,
  846. * or a negative error code.
  847. */
  848. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  849. __u32 start_minor_hash, __u32 *next_hash)
  850. {
  851. struct dx_hash_info hinfo;
  852. struct ext4_dir_entry_2 *de;
  853. struct dx_frame frames[2], *frame;
  854. struct inode *dir;
  855. ext4_lblk_t block;
  856. int count = 0;
  857. int ret, err;
  858. __u32 hashval;
  859. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  860. start_hash, start_minor_hash));
  861. dir = file_inode(dir_file);
  862. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  863. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  864. if (hinfo.hash_version <= DX_HASH_TEA)
  865. hinfo.hash_version +=
  866. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  867. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  868. if (ext4_has_inline_data(dir)) {
  869. int has_inline_data = 1;
  870. count = htree_inlinedir_to_tree(dir_file, dir, 0,
  871. &hinfo, start_hash,
  872. start_minor_hash,
  873. &has_inline_data);
  874. if (has_inline_data) {
  875. *next_hash = ~0;
  876. return count;
  877. }
  878. }
  879. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  880. start_hash, start_minor_hash);
  881. *next_hash = ~0;
  882. return count;
  883. }
  884. hinfo.hash = start_hash;
  885. hinfo.minor_hash = 0;
  886. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  887. if (!frame)
  888. return err;
  889. /* Add '.' and '..' from the htree header */
  890. if (!start_hash && !start_minor_hash) {
  891. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  892. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  893. goto errout;
  894. count++;
  895. }
  896. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  897. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  898. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  899. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  900. goto errout;
  901. count++;
  902. }
  903. while (1) {
  904. block = dx_get_block(frame->at);
  905. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  906. start_hash, start_minor_hash);
  907. if (ret < 0) {
  908. err = ret;
  909. goto errout;
  910. }
  911. count += ret;
  912. hashval = ~0;
  913. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  914. frame, frames, &hashval);
  915. *next_hash = hashval;
  916. if (ret < 0) {
  917. err = ret;
  918. goto errout;
  919. }
  920. /*
  921. * Stop if: (a) there are no more entries, or
  922. * (b) we have inserted at least one entry and the
  923. * next hash value is not a continuation
  924. */
  925. if ((ret == 0) ||
  926. (count && ((hashval & 1) == 0)))
  927. break;
  928. }
  929. dx_release(frames);
  930. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  931. "next hash: %x\n", count, *next_hash));
  932. return count;
  933. errout:
  934. dx_release(frames);
  935. return (err);
  936. }
  937. static inline int search_dirblock(struct buffer_head *bh,
  938. struct inode *dir,
  939. const struct qstr *d_name,
  940. unsigned int offset,
  941. struct ext4_dir_entry_2 **res_dir)
  942. {
  943. return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
  944. d_name, offset, res_dir);
  945. }
  946. /*
  947. * Directory block splitting, compacting
  948. */
  949. /*
  950. * Create map of hash values, offsets, and sizes, stored at end of block.
  951. * Returns number of entries mapped.
  952. */
  953. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  954. struct dx_hash_info *hinfo,
  955. struct dx_map_entry *map_tail)
  956. {
  957. int count = 0;
  958. char *base = (char *) de;
  959. struct dx_hash_info h = *hinfo;
  960. while ((char *) de < base + blocksize) {
  961. if (de->name_len && de->inode) {
  962. ext4fs_dirhash(de->name, de->name_len, &h);
  963. map_tail--;
  964. map_tail->hash = h.hash;
  965. map_tail->offs = ((char *) de - base)>>2;
  966. map_tail->size = le16_to_cpu(de->rec_len);
  967. count++;
  968. cond_resched();
  969. }
  970. /* XXX: do we need to check rec_len == 0 case? -Chris */
  971. de = ext4_next_entry(de, blocksize);
  972. }
  973. return count;
  974. }
  975. /* Sort map by hash value */
  976. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  977. {
  978. struct dx_map_entry *p, *q, *top = map + count - 1;
  979. int more;
  980. /* Combsort until bubble sort doesn't suck */
  981. while (count > 2) {
  982. count = count*10/13;
  983. if (count - 9 < 2) /* 9, 10 -> 11 */
  984. count = 11;
  985. for (p = top, q = p - count; q >= map; p--, q--)
  986. if (p->hash < q->hash)
  987. swap(*p, *q);
  988. }
  989. /* Garden variety bubble sort */
  990. do {
  991. more = 0;
  992. q = top;
  993. while (q-- > map) {
  994. if (q[1].hash >= q[0].hash)
  995. continue;
  996. swap(*(q+1), *q);
  997. more = 1;
  998. }
  999. } while(more);
  1000. }
  1001. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  1002. {
  1003. struct dx_entry *entries = frame->entries;
  1004. struct dx_entry *old = frame->at, *new = old + 1;
  1005. int count = dx_get_count(entries);
  1006. assert(count < dx_get_limit(entries));
  1007. assert(old < entries + count);
  1008. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  1009. dx_set_hash(new, hash);
  1010. dx_set_block(new, block);
  1011. dx_set_count(entries, count + 1);
  1012. }
  1013. /*
  1014. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  1015. *
  1016. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  1017. * `de != NULL' is guaranteed by caller.
  1018. */
  1019. static inline int ext4_match (int len, const char * const name,
  1020. struct ext4_dir_entry_2 * de)
  1021. {
  1022. if (len != de->name_len)
  1023. return 0;
  1024. if (!de->inode)
  1025. return 0;
  1026. return !memcmp(name, de->name, len);
  1027. }
  1028. /*
  1029. * Returns 0 if not found, -1 on failure, and 1 on success
  1030. */
  1031. int search_dir(struct buffer_head *bh,
  1032. char *search_buf,
  1033. int buf_size,
  1034. struct inode *dir,
  1035. const struct qstr *d_name,
  1036. unsigned int offset,
  1037. struct ext4_dir_entry_2 **res_dir)
  1038. {
  1039. struct ext4_dir_entry_2 * de;
  1040. char * dlimit;
  1041. int de_len;
  1042. const char *name = d_name->name;
  1043. int namelen = d_name->len;
  1044. de = (struct ext4_dir_entry_2 *)search_buf;
  1045. dlimit = search_buf + buf_size;
  1046. while ((char *) de < dlimit) {
  1047. /* this code is executed quadratically often */
  1048. /* do minimal checking `by hand' */
  1049. if ((char *) de + namelen <= dlimit &&
  1050. ext4_match (namelen, name, de)) {
  1051. /* found a match - just to be sure, do a full check */
  1052. if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
  1053. bh->b_size, offset))
  1054. return -1;
  1055. *res_dir = de;
  1056. return 1;
  1057. }
  1058. /* prevent looping on a bad block */
  1059. de_len = ext4_rec_len_from_disk(de->rec_len,
  1060. dir->i_sb->s_blocksize);
  1061. if (de_len <= 0)
  1062. return -1;
  1063. offset += de_len;
  1064. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  1065. }
  1066. return 0;
  1067. }
  1068. static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
  1069. struct ext4_dir_entry *de)
  1070. {
  1071. struct super_block *sb = dir->i_sb;
  1072. if (!is_dx(dir))
  1073. return 0;
  1074. if (block == 0)
  1075. return 1;
  1076. if (de->inode == 0 &&
  1077. ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
  1078. sb->s_blocksize)
  1079. return 1;
  1080. return 0;
  1081. }
  1082. /*
  1083. * ext4_find_entry()
  1084. *
  1085. * finds an entry in the specified directory with the wanted name. It
  1086. * returns the cache buffer in which the entry was found, and the entry
  1087. * itself (as a parameter - res_dir). It does NOT read the inode of the
  1088. * entry - you'll have to do that yourself if you want to.
  1089. *
  1090. * The returned buffer_head has ->b_count elevated. The caller is expected
  1091. * to brelse() it when appropriate.
  1092. */
  1093. static struct buffer_head * ext4_find_entry (struct inode *dir,
  1094. const struct qstr *d_name,
  1095. struct ext4_dir_entry_2 **res_dir,
  1096. int *inlined)
  1097. {
  1098. struct super_block *sb;
  1099. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1100. struct buffer_head *bh, *ret = NULL;
  1101. ext4_lblk_t start, block, b;
  1102. const u8 *name = d_name->name;
  1103. int ra_max = 0; /* Number of bh's in the readahead
  1104. buffer, bh_use[] */
  1105. int ra_ptr = 0; /* Current index into readahead
  1106. buffer */
  1107. int num = 0;
  1108. ext4_lblk_t nblocks;
  1109. int i, err;
  1110. int namelen;
  1111. *res_dir = NULL;
  1112. sb = dir->i_sb;
  1113. namelen = d_name->len;
  1114. if (namelen > EXT4_NAME_LEN)
  1115. return NULL;
  1116. if (ext4_has_inline_data(dir)) {
  1117. int has_inline_data = 1;
  1118. ret = ext4_find_inline_entry(dir, d_name, res_dir,
  1119. &has_inline_data);
  1120. if (has_inline_data) {
  1121. if (inlined)
  1122. *inlined = 1;
  1123. return ret;
  1124. }
  1125. }
  1126. if ((namelen <= 2) && (name[0] == '.') &&
  1127. (name[1] == '.' || name[1] == '\0')) {
  1128. /*
  1129. * "." or ".." will only be in the first block
  1130. * NFS may look up ".."; "." should be handled by the VFS
  1131. */
  1132. block = start = 0;
  1133. nblocks = 1;
  1134. goto restart;
  1135. }
  1136. if (is_dx(dir)) {
  1137. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  1138. /*
  1139. * On success, or if the error was file not found,
  1140. * return. Otherwise, fall back to doing a search the
  1141. * old fashioned way.
  1142. */
  1143. if (bh || (err != ERR_BAD_DX_DIR))
  1144. return bh;
  1145. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  1146. "falling back\n"));
  1147. }
  1148. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1149. start = EXT4_I(dir)->i_dir_start_lookup;
  1150. if (start >= nblocks)
  1151. start = 0;
  1152. block = start;
  1153. restart:
  1154. do {
  1155. /*
  1156. * We deal with the read-ahead logic here.
  1157. */
  1158. if (ra_ptr >= ra_max) {
  1159. /* Refill the readahead buffer */
  1160. ra_ptr = 0;
  1161. b = block;
  1162. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  1163. /*
  1164. * Terminate if we reach the end of the
  1165. * directory and must wrap, or if our
  1166. * search has finished at this block.
  1167. */
  1168. if (b >= nblocks || (num && block == start)) {
  1169. bh_use[ra_max] = NULL;
  1170. break;
  1171. }
  1172. num++;
  1173. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  1174. bh_use[ra_max] = bh;
  1175. if (bh)
  1176. ll_rw_block(READ | REQ_META | REQ_PRIO,
  1177. 1, &bh);
  1178. }
  1179. }
  1180. if ((bh = bh_use[ra_ptr++]) == NULL)
  1181. goto next;
  1182. wait_on_buffer(bh);
  1183. if (!buffer_uptodate(bh)) {
  1184. /* read error, skip block & hope for the best */
  1185. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  1186. (unsigned long) block);
  1187. brelse(bh);
  1188. goto next;
  1189. }
  1190. if (!buffer_verified(bh) &&
  1191. !is_dx_internal_node(dir, block,
  1192. (struct ext4_dir_entry *)bh->b_data) &&
  1193. !ext4_dirent_csum_verify(dir,
  1194. (struct ext4_dir_entry *)bh->b_data)) {
  1195. EXT4_ERROR_INODE(dir, "checksumming directory "
  1196. "block %lu", (unsigned long)block);
  1197. brelse(bh);
  1198. goto next;
  1199. }
  1200. set_buffer_verified(bh);
  1201. i = search_dirblock(bh, dir, d_name,
  1202. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  1203. if (i == 1) {
  1204. EXT4_I(dir)->i_dir_start_lookup = block;
  1205. ret = bh;
  1206. goto cleanup_and_exit;
  1207. } else {
  1208. brelse(bh);
  1209. if (i < 0)
  1210. goto cleanup_and_exit;
  1211. }
  1212. next:
  1213. if (++block >= nblocks)
  1214. block = 0;
  1215. } while (block != start);
  1216. /*
  1217. * If the directory has grown while we were searching, then
  1218. * search the last part of the directory before giving up.
  1219. */
  1220. block = nblocks;
  1221. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1222. if (block < nblocks) {
  1223. start = 0;
  1224. goto restart;
  1225. }
  1226. cleanup_and_exit:
  1227. /* Clean up the read-ahead blocks */
  1228. for (; ra_ptr < ra_max; ra_ptr++)
  1229. brelse(bh_use[ra_ptr]);
  1230. return ret;
  1231. }
  1232. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  1233. struct ext4_dir_entry_2 **res_dir, int *err)
  1234. {
  1235. struct super_block * sb = dir->i_sb;
  1236. struct dx_hash_info hinfo;
  1237. struct dx_frame frames[2], *frame;
  1238. struct buffer_head *bh;
  1239. ext4_lblk_t block;
  1240. int retval;
  1241. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  1242. return NULL;
  1243. do {
  1244. block = dx_get_block(frame->at);
  1245. bh = ext4_read_dirblock(dir, block, DIRENT);
  1246. if (IS_ERR(bh)) {
  1247. *err = PTR_ERR(bh);
  1248. goto errout;
  1249. }
  1250. retval = search_dirblock(bh, dir, d_name,
  1251. block << EXT4_BLOCK_SIZE_BITS(sb),
  1252. res_dir);
  1253. if (retval == 1) { /* Success! */
  1254. dx_release(frames);
  1255. return bh;
  1256. }
  1257. brelse(bh);
  1258. if (retval == -1) {
  1259. *err = ERR_BAD_DX_DIR;
  1260. goto errout;
  1261. }
  1262. /* Check to see if we should continue to search */
  1263. retval = ext4_htree_next_block(dir, hinfo.hash, frame,
  1264. frames, NULL);
  1265. if (retval < 0) {
  1266. ext4_warning(sb,
  1267. "error reading index page in directory #%lu",
  1268. dir->i_ino);
  1269. *err = retval;
  1270. goto errout;
  1271. }
  1272. } while (retval == 1);
  1273. *err = -ENOENT;
  1274. errout:
  1275. dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
  1276. dx_release (frames);
  1277. return NULL;
  1278. }
  1279. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1280. {
  1281. struct inode *inode;
  1282. struct ext4_dir_entry_2 *de;
  1283. struct buffer_head *bh;
  1284. if (dentry->d_name.len > EXT4_NAME_LEN)
  1285. return ERR_PTR(-ENAMETOOLONG);
  1286. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  1287. inode = NULL;
  1288. if (bh) {
  1289. __u32 ino = le32_to_cpu(de->inode);
  1290. brelse(bh);
  1291. if (!ext4_valid_inum(dir->i_sb, ino)) {
  1292. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  1293. return ERR_PTR(-EIO);
  1294. }
  1295. if (unlikely(ino == dir->i_ino)) {
  1296. EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
  1297. dentry);
  1298. return ERR_PTR(-EIO);
  1299. }
  1300. inode = ext4_iget(dir->i_sb, ino);
  1301. if (inode == ERR_PTR(-ESTALE)) {
  1302. EXT4_ERROR_INODE(dir,
  1303. "deleted inode referenced: %u",
  1304. ino);
  1305. return ERR_PTR(-EIO);
  1306. }
  1307. }
  1308. return d_splice_alias(inode, dentry);
  1309. }
  1310. struct dentry *ext4_get_parent(struct dentry *child)
  1311. {
  1312. __u32 ino;
  1313. static const struct qstr dotdot = QSTR_INIT("..", 2);
  1314. struct ext4_dir_entry_2 * de;
  1315. struct buffer_head *bh;
  1316. bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
  1317. if (!bh)
  1318. return ERR_PTR(-ENOENT);
  1319. ino = le32_to_cpu(de->inode);
  1320. brelse(bh);
  1321. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  1322. EXT4_ERROR_INODE(child->d_inode,
  1323. "bad parent inode number: %u", ino);
  1324. return ERR_PTR(-EIO);
  1325. }
  1326. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  1327. }
  1328. /*
  1329. * Move count entries from end of map between two memory locations.
  1330. * Returns pointer to last entry moved.
  1331. */
  1332. static struct ext4_dir_entry_2 *
  1333. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1334. unsigned blocksize)
  1335. {
  1336. unsigned rec_len = 0;
  1337. while (count--) {
  1338. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1339. (from + (map->offs<<2));
  1340. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1341. memcpy (to, de, rec_len);
  1342. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1343. ext4_rec_len_to_disk(rec_len, blocksize);
  1344. de->inode = 0;
  1345. map++;
  1346. to += rec_len;
  1347. }
  1348. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1349. }
  1350. /*
  1351. * Compact each dir entry in the range to the minimal rec_len.
  1352. * Returns pointer to last entry in range.
  1353. */
  1354. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1355. {
  1356. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1357. unsigned rec_len = 0;
  1358. prev = to = de;
  1359. while ((char*)de < base + blocksize) {
  1360. next = ext4_next_entry(de, blocksize);
  1361. if (de->inode && de->name_len) {
  1362. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1363. if (de > to)
  1364. memmove(to, de, rec_len);
  1365. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1366. prev = to;
  1367. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1368. }
  1369. de = next;
  1370. }
  1371. return prev;
  1372. }
  1373. /*
  1374. * Split a full leaf block to make room for a new dir entry.
  1375. * Allocate a new block, and move entries so that they are approx. equally full.
  1376. * Returns pointer to de in block into which the new entry will be inserted.
  1377. */
  1378. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1379. struct buffer_head **bh,struct dx_frame *frame,
  1380. struct dx_hash_info *hinfo, int *error)
  1381. {
  1382. unsigned blocksize = dir->i_sb->s_blocksize;
  1383. unsigned count, continued;
  1384. struct buffer_head *bh2;
  1385. ext4_lblk_t newblock;
  1386. u32 hash2;
  1387. struct dx_map_entry *map;
  1388. char *data1 = (*bh)->b_data, *data2;
  1389. unsigned split, move, size;
  1390. struct ext4_dir_entry_2 *de = NULL, *de2;
  1391. struct ext4_dir_entry_tail *t;
  1392. int csum_size = 0;
  1393. int err = 0, i;
  1394. if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
  1395. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  1396. csum_size = sizeof(struct ext4_dir_entry_tail);
  1397. bh2 = ext4_append(handle, dir, &newblock);
  1398. if (IS_ERR(bh2)) {
  1399. brelse(*bh);
  1400. *bh = NULL;
  1401. *error = PTR_ERR(bh2);
  1402. return NULL;
  1403. }
  1404. BUFFER_TRACE(*bh, "get_write_access");
  1405. err = ext4_journal_get_write_access(handle, *bh);
  1406. if (err)
  1407. goto journal_error;
  1408. BUFFER_TRACE(frame->bh, "get_write_access");
  1409. err = ext4_journal_get_write_access(handle, frame->bh);
  1410. if (err)
  1411. goto journal_error;
  1412. data2 = bh2->b_data;
  1413. /* create map in the end of data2 block */
  1414. map = (struct dx_map_entry *) (data2 + blocksize);
  1415. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1416. blocksize, hinfo, map);
  1417. map -= count;
  1418. dx_sort_map(map, count);
  1419. /* Split the existing block in the middle, size-wise */
  1420. size = 0;
  1421. move = 0;
  1422. for (i = count-1; i >= 0; i--) {
  1423. /* is more than half of this entry in 2nd half of the block? */
  1424. if (size + map[i].size/2 > blocksize/2)
  1425. break;
  1426. size += map[i].size;
  1427. move++;
  1428. }
  1429. /* map index at which we will split */
  1430. split = count - move;
  1431. hash2 = map[split].hash;
  1432. continued = hash2 == map[split - 1].hash;
  1433. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1434. (unsigned long)dx_get_block(frame->at),
  1435. hash2, split, count-split));
  1436. /* Fancy dance to stay within two buffers */
  1437. de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
  1438. de = dx_pack_dirents(data1, blocksize);
  1439. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1440. (char *) de,
  1441. blocksize);
  1442. de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  1443. (char *) de2,
  1444. blocksize);
  1445. if (csum_size) {
  1446. t = EXT4_DIRENT_TAIL(data2, blocksize);
  1447. initialize_dirent_tail(t, blocksize);
  1448. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1449. initialize_dirent_tail(t, blocksize);
  1450. }
  1451. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1452. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1453. /* Which block gets the new entry? */
  1454. if (hinfo->hash >= hash2)
  1455. {
  1456. swap(*bh, bh2);
  1457. de = de2;
  1458. }
  1459. dx_insert_block(frame, hash2 + continued, newblock);
  1460. err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1461. if (err)
  1462. goto journal_error;
  1463. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1464. if (err)
  1465. goto journal_error;
  1466. brelse(bh2);
  1467. dxtrace(dx_show_index("frame", frame->entries));
  1468. return de;
  1469. journal_error:
  1470. brelse(*bh);
  1471. brelse(bh2);
  1472. *bh = NULL;
  1473. ext4_std_error(dir->i_sb, err);
  1474. *error = err;
  1475. return NULL;
  1476. }
  1477. int ext4_find_dest_de(struct inode *dir, struct inode *inode,
  1478. struct buffer_head *bh,
  1479. void *buf, int buf_size,
  1480. const char *name, int namelen,
  1481. struct ext4_dir_entry_2 **dest_de)
  1482. {
  1483. struct ext4_dir_entry_2 *de;
  1484. unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
  1485. int nlen, rlen;
  1486. unsigned int offset = 0;
  1487. char *top;
  1488. de = (struct ext4_dir_entry_2 *)buf;
  1489. top = buf + buf_size - reclen;
  1490. while ((char *) de <= top) {
  1491. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1492. buf, buf_size, offset))
  1493. return -EIO;
  1494. if (ext4_match(namelen, name, de))
  1495. return -EEXIST;
  1496. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1497. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1498. if ((de->inode ? rlen - nlen : rlen) >= reclen)
  1499. break;
  1500. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1501. offset += rlen;
  1502. }
  1503. if ((char *) de > top)
  1504. return -ENOSPC;
  1505. *dest_de = de;
  1506. return 0;
  1507. }
  1508. void ext4_insert_dentry(struct inode *inode,
  1509. struct ext4_dir_entry_2 *de,
  1510. int buf_size,
  1511. const char *name, int namelen)
  1512. {
  1513. int nlen, rlen;
  1514. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1515. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1516. if (de->inode) {
  1517. struct ext4_dir_entry_2 *de1 =
  1518. (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1519. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
  1520. de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
  1521. de = de1;
  1522. }
  1523. de->file_type = EXT4_FT_UNKNOWN;
  1524. de->inode = cpu_to_le32(inode->i_ino);
  1525. ext4_set_de_type(inode->i_sb, de, inode->i_mode);
  1526. de->name_len = namelen;
  1527. memcpy(de->name, name, namelen);
  1528. }
  1529. /*
  1530. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1531. * it points to a directory entry which is guaranteed to be large
  1532. * enough for new directory entry. If de is NULL, then
  1533. * add_dirent_to_buf will attempt search the directory block for
  1534. * space. It will return -ENOSPC if no space is available, and -EIO
  1535. * and -EEXIST if directory entry already exists.
  1536. */
  1537. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1538. struct inode *inode, struct ext4_dir_entry_2 *de,
  1539. struct buffer_head *bh)
  1540. {
  1541. struct inode *dir = dentry->d_parent->d_inode;
  1542. const char *name = dentry->d_name.name;
  1543. int namelen = dentry->d_name.len;
  1544. unsigned int blocksize = dir->i_sb->s_blocksize;
  1545. int csum_size = 0;
  1546. int err;
  1547. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  1548. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  1549. csum_size = sizeof(struct ext4_dir_entry_tail);
  1550. if (!de) {
  1551. err = ext4_find_dest_de(dir, inode,
  1552. bh, bh->b_data, blocksize - csum_size,
  1553. name, namelen, &de);
  1554. if (err)
  1555. return err;
  1556. }
  1557. BUFFER_TRACE(bh, "get_write_access");
  1558. err = ext4_journal_get_write_access(handle, bh);
  1559. if (err) {
  1560. ext4_std_error(dir->i_sb, err);
  1561. return err;
  1562. }
  1563. /* By now the buffer is marked for journaling */
  1564. ext4_insert_dentry(inode, de, blocksize, name, namelen);
  1565. /*
  1566. * XXX shouldn't update any times until successful
  1567. * completion of syscall, but too many callers depend
  1568. * on this.
  1569. *
  1570. * XXX similarly, too many callers depend on
  1571. * ext4_new_inode() setting the times, but error
  1572. * recovery deletes the inode, so the worst that can
  1573. * happen is that the times are slightly out of date
  1574. * and/or different from the directory change time.
  1575. */
  1576. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1577. ext4_update_dx_flag(dir);
  1578. dir->i_version++;
  1579. ext4_mark_inode_dirty(handle, dir);
  1580. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1581. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1582. if (err)
  1583. ext4_std_error(dir->i_sb, err);
  1584. return 0;
  1585. }
  1586. /*
  1587. * This converts a one block unindexed directory to a 3 block indexed
  1588. * directory, and adds the dentry to the indexed directory.
  1589. */
  1590. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1591. struct inode *inode, struct buffer_head *bh)
  1592. {
  1593. struct inode *dir = dentry->d_parent->d_inode;
  1594. const char *name = dentry->d_name.name;
  1595. int namelen = dentry->d_name.len;
  1596. struct buffer_head *bh2;
  1597. struct dx_root *root;
  1598. struct dx_frame frames[2], *frame;
  1599. struct dx_entry *entries;
  1600. struct ext4_dir_entry_2 *de, *de2;
  1601. struct ext4_dir_entry_tail *t;
  1602. char *data1, *top;
  1603. unsigned len;
  1604. int retval;
  1605. unsigned blocksize;
  1606. struct dx_hash_info hinfo;
  1607. ext4_lblk_t block;
  1608. struct fake_dirent *fde;
  1609. int csum_size = 0;
  1610. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  1611. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  1612. csum_size = sizeof(struct ext4_dir_entry_tail);
  1613. blocksize = dir->i_sb->s_blocksize;
  1614. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1615. retval = ext4_journal_get_write_access(handle, bh);
  1616. if (retval) {
  1617. ext4_std_error(dir->i_sb, retval);
  1618. brelse(bh);
  1619. return retval;
  1620. }
  1621. root = (struct dx_root *) bh->b_data;
  1622. /* The 0th block becomes the root, move the dirents out */
  1623. fde = &root->dotdot;
  1624. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1625. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1626. if ((char *) de >= (((char *) root) + blocksize)) {
  1627. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1628. brelse(bh);
  1629. return -EIO;
  1630. }
  1631. len = ((char *) root) + (blocksize - csum_size) - (char *) de;
  1632. /* Allocate new block for the 0th block's dirents */
  1633. bh2 = ext4_append(handle, dir, &block);
  1634. if (IS_ERR(bh2)) {
  1635. brelse(bh);
  1636. return PTR_ERR(bh2);
  1637. }
  1638. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1639. data1 = bh2->b_data;
  1640. memcpy (data1, de, len);
  1641. de = (struct ext4_dir_entry_2 *) data1;
  1642. top = data1 + len;
  1643. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1644. de = de2;
  1645. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1646. (char *) de,
  1647. blocksize);
  1648. if (csum_size) {
  1649. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1650. initialize_dirent_tail(t, blocksize);
  1651. }
  1652. /* Initialize the root; the dot dirents already exist */
  1653. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1654. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1655. blocksize);
  1656. memset (&root->info, 0, sizeof(root->info));
  1657. root->info.info_length = sizeof(root->info);
  1658. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1659. entries = root->entries;
  1660. dx_set_block(entries, 1);
  1661. dx_set_count(entries, 1);
  1662. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1663. /* Initialize as for dx_probe */
  1664. hinfo.hash_version = root->info.hash_version;
  1665. if (hinfo.hash_version <= DX_HASH_TEA)
  1666. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1667. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1668. ext4fs_dirhash(name, namelen, &hinfo);
  1669. frame = frames;
  1670. frame->entries = entries;
  1671. frame->at = entries;
  1672. frame->bh = bh;
  1673. bh = bh2;
  1674. ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1675. ext4_handle_dirty_dirent_node(handle, dir, bh);
  1676. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1677. if (!de) {
  1678. /*
  1679. * Even if the block split failed, we have to properly write
  1680. * out all the changes we did so far. Otherwise we can end up
  1681. * with corrupted filesystem.
  1682. */
  1683. ext4_mark_inode_dirty(handle, dir);
  1684. dx_release(frames);
  1685. return retval;
  1686. }
  1687. dx_release(frames);
  1688. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1689. brelse(bh);
  1690. return retval;
  1691. }
  1692. /*
  1693. * ext4_add_entry()
  1694. *
  1695. * adds a file entry to the specified directory, using the same
  1696. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1697. *
  1698. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1699. * may not sleep between calling this and putting something into
  1700. * the entry, as someone else might have used it while you slept.
  1701. */
  1702. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1703. struct inode *inode)
  1704. {
  1705. struct inode *dir = dentry->d_parent->d_inode;
  1706. struct buffer_head *bh;
  1707. struct ext4_dir_entry_2 *de;
  1708. struct ext4_dir_entry_tail *t;
  1709. struct super_block *sb;
  1710. int retval;
  1711. int dx_fallback=0;
  1712. unsigned blocksize;
  1713. ext4_lblk_t block, blocks;
  1714. int csum_size = 0;
  1715. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  1716. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  1717. csum_size = sizeof(struct ext4_dir_entry_tail);
  1718. sb = dir->i_sb;
  1719. blocksize = sb->s_blocksize;
  1720. if (!dentry->d_name.len)
  1721. return -EINVAL;
  1722. if (ext4_has_inline_data(dir)) {
  1723. retval = ext4_try_add_inline_entry(handle, dentry, inode);
  1724. if (retval < 0)
  1725. return retval;
  1726. if (retval == 1) {
  1727. retval = 0;
  1728. return retval;
  1729. }
  1730. }
  1731. if (is_dx(dir)) {
  1732. retval = ext4_dx_add_entry(handle, dentry, inode);
  1733. if (!retval || (retval != ERR_BAD_DX_DIR))
  1734. return retval;
  1735. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1736. dx_fallback++;
  1737. ext4_mark_inode_dirty(handle, dir);
  1738. }
  1739. blocks = dir->i_size >> sb->s_blocksize_bits;
  1740. for (block = 0; block < blocks; block++) {
  1741. bh = ext4_read_dirblock(dir, block, DIRENT);
  1742. if (IS_ERR(bh))
  1743. return PTR_ERR(bh);
  1744. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1745. if (retval != -ENOSPC) {
  1746. brelse(bh);
  1747. return retval;
  1748. }
  1749. if (blocks == 1 && !dx_fallback &&
  1750. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1751. return make_indexed_dir(handle, dentry, inode, bh);
  1752. brelse(bh);
  1753. }
  1754. bh = ext4_append(handle, dir, &block);
  1755. if (IS_ERR(bh))
  1756. return PTR_ERR(bh);
  1757. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1758. de->inode = 0;
  1759. de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
  1760. if (csum_size) {
  1761. t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
  1762. initialize_dirent_tail(t, blocksize);
  1763. }
  1764. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1765. brelse(bh);
  1766. if (retval == 0)
  1767. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1768. return retval;
  1769. }
  1770. /*
  1771. * Returns 0 for success, or a negative error value
  1772. */
  1773. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1774. struct inode *inode)
  1775. {
  1776. struct dx_frame frames[2], *frame;
  1777. struct dx_entry *entries, *at;
  1778. struct dx_hash_info hinfo;
  1779. struct buffer_head *bh;
  1780. struct inode *dir = dentry->d_parent->d_inode;
  1781. struct super_block *sb = dir->i_sb;
  1782. struct ext4_dir_entry_2 *de;
  1783. int err;
  1784. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1785. if (!frame)
  1786. return err;
  1787. entries = frame->entries;
  1788. at = frame->at;
  1789. bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
  1790. if (IS_ERR(bh)) {
  1791. err = PTR_ERR(bh);
  1792. bh = NULL;
  1793. goto cleanup;
  1794. }
  1795. BUFFER_TRACE(bh, "get_write_access");
  1796. err = ext4_journal_get_write_access(handle, bh);
  1797. if (err)
  1798. goto journal_error;
  1799. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1800. if (err != -ENOSPC)
  1801. goto cleanup;
  1802. /* Block full, should compress but for now just split */
  1803. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1804. dx_get_count(entries), dx_get_limit(entries)));
  1805. /* Need to split index? */
  1806. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1807. ext4_lblk_t newblock;
  1808. unsigned icount = dx_get_count(entries);
  1809. int levels = frame - frames;
  1810. struct dx_entry *entries2;
  1811. struct dx_node *node2;
  1812. struct buffer_head *bh2;
  1813. if (levels && (dx_get_count(frames->entries) ==
  1814. dx_get_limit(frames->entries))) {
  1815. ext4_warning(sb, "Directory index full!");
  1816. err = -ENOSPC;
  1817. goto cleanup;
  1818. }
  1819. bh2 = ext4_append(handle, dir, &newblock);
  1820. if (IS_ERR(bh2)) {
  1821. err = PTR_ERR(bh2);
  1822. goto cleanup;
  1823. }
  1824. node2 = (struct dx_node *)(bh2->b_data);
  1825. entries2 = node2->entries;
  1826. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  1827. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  1828. sb->s_blocksize);
  1829. BUFFER_TRACE(frame->bh, "get_write_access");
  1830. err = ext4_journal_get_write_access(handle, frame->bh);
  1831. if (err)
  1832. goto journal_error;
  1833. if (levels) {
  1834. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1835. unsigned hash2 = dx_get_hash(entries + icount1);
  1836. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1837. icount1, icount2));
  1838. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1839. err = ext4_journal_get_write_access(handle,
  1840. frames[0].bh);
  1841. if (err)
  1842. goto journal_error;
  1843. memcpy((char *) entries2, (char *) (entries + icount1),
  1844. icount2 * sizeof(struct dx_entry));
  1845. dx_set_count(entries, icount1);
  1846. dx_set_count(entries2, icount2);
  1847. dx_set_limit(entries2, dx_node_limit(dir));
  1848. /* Which index block gets the new entry? */
  1849. if (at - entries >= icount1) {
  1850. frame->at = at = at - entries - icount1 + entries2;
  1851. frame->entries = entries = entries2;
  1852. swap(frame->bh, bh2);
  1853. }
  1854. dx_insert_block(frames + 0, hash2, newblock);
  1855. dxtrace(dx_show_index("node", frames[1].entries));
  1856. dxtrace(dx_show_index("node",
  1857. ((struct dx_node *) bh2->b_data)->entries));
  1858. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  1859. if (err)
  1860. goto journal_error;
  1861. brelse (bh2);
  1862. } else {
  1863. dxtrace(printk(KERN_DEBUG
  1864. "Creating second level index...\n"));
  1865. memcpy((char *) entries2, (char *) entries,
  1866. icount * sizeof(struct dx_entry));
  1867. dx_set_limit(entries2, dx_node_limit(dir));
  1868. /* Set up root */
  1869. dx_set_count(entries, 1);
  1870. dx_set_block(entries + 0, newblock);
  1871. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1872. /* Add new access path frame */
  1873. frame = frames + 1;
  1874. frame->at = at = at - entries + entries2;
  1875. frame->entries = entries = entries2;
  1876. frame->bh = bh2;
  1877. err = ext4_journal_get_write_access(handle,
  1878. frame->bh);
  1879. if (err)
  1880. goto journal_error;
  1881. }
  1882. err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
  1883. if (err) {
  1884. ext4_std_error(inode->i_sb, err);
  1885. goto cleanup;
  1886. }
  1887. }
  1888. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1889. if (!de)
  1890. goto cleanup;
  1891. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1892. goto cleanup;
  1893. journal_error:
  1894. ext4_std_error(dir->i_sb, err);
  1895. cleanup:
  1896. brelse(bh);
  1897. dx_release(frames);
  1898. return err;
  1899. }
  1900. /*
  1901. * ext4_generic_delete_entry deletes a directory entry by merging it
  1902. * with the previous entry
  1903. */
  1904. int ext4_generic_delete_entry(handle_t *handle,
  1905. struct inode *dir,
  1906. struct ext4_dir_entry_2 *de_del,
  1907. struct buffer_head *bh,
  1908. void *entry_buf,
  1909. int buf_size,
  1910. int csum_size)
  1911. {
  1912. struct ext4_dir_entry_2 *de, *pde;
  1913. unsigned int blocksize = dir->i_sb->s_blocksize;
  1914. int i;
  1915. i = 0;
  1916. pde = NULL;
  1917. de = (struct ext4_dir_entry_2 *)entry_buf;
  1918. while (i < buf_size - csum_size) {
  1919. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1920. bh->b_data, bh->b_size, i))
  1921. return -EIO;
  1922. if (de == de_del) {
  1923. if (pde)
  1924. pde->rec_len = ext4_rec_len_to_disk(
  1925. ext4_rec_len_from_disk(pde->rec_len,
  1926. blocksize) +
  1927. ext4_rec_len_from_disk(de->rec_len,
  1928. blocksize),
  1929. blocksize);
  1930. else
  1931. de->inode = 0;
  1932. dir->i_version++;
  1933. return 0;
  1934. }
  1935. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  1936. pde = de;
  1937. de = ext4_next_entry(de, blocksize);
  1938. }
  1939. return -ENOENT;
  1940. }
  1941. static int ext4_delete_entry(handle_t *handle,
  1942. struct inode *dir,
  1943. struct ext4_dir_entry_2 *de_del,
  1944. struct buffer_head *bh)
  1945. {
  1946. int err, csum_size = 0;
  1947. if (ext4_has_inline_data(dir)) {
  1948. int has_inline_data = 1;
  1949. err = ext4_delete_inline_entry(handle, dir, de_del, bh,
  1950. &has_inline_data);
  1951. if (has_inline_data)
  1952. return err;
  1953. }
  1954. if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
  1955. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  1956. csum_size = sizeof(struct ext4_dir_entry_tail);
  1957. BUFFER_TRACE(bh, "get_write_access");
  1958. err = ext4_journal_get_write_access(handle, bh);
  1959. if (unlikely(err))
  1960. goto out;
  1961. err = ext4_generic_delete_entry(handle, dir, de_del,
  1962. bh, bh->b_data,
  1963. dir->i_sb->s_blocksize, csum_size);
  1964. if (err)
  1965. goto out;
  1966. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1967. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1968. if (unlikely(err))
  1969. goto out;
  1970. return 0;
  1971. out:
  1972. if (err != -ENOENT)
  1973. ext4_std_error(dir->i_sb, err);
  1974. return err;
  1975. }
  1976. /*
  1977. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1978. * since this indicates that nlinks count was previously 1.
  1979. */
  1980. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1981. {
  1982. inc_nlink(inode);
  1983. if (is_dx(inode) && inode->i_nlink > 1) {
  1984. /* limit is 16-bit i_links_count */
  1985. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1986. set_nlink(inode, 1);
  1987. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1988. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1989. }
  1990. }
  1991. }
  1992. /*
  1993. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1994. * directory has >EXT4_LINK_MAX subdirs.
  1995. */
  1996. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1997. {
  1998. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  1999. drop_nlink(inode);
  2000. }
  2001. static int ext4_add_nondir(handle_t *handle,
  2002. struct dentry *dentry, struct inode *inode)
  2003. {
  2004. int err = ext4_add_entry(handle, dentry, inode);
  2005. if (!err) {
  2006. ext4_mark_inode_dirty(handle, inode);
  2007. unlock_new_inode(inode);
  2008. d_instantiate(dentry, inode);
  2009. return 0;
  2010. }
  2011. drop_nlink(inode);
  2012. unlock_new_inode(inode);
  2013. iput(inode);
  2014. return err;
  2015. }
  2016. /*
  2017. * By the time this is called, we already have created
  2018. * the directory cache entry for the new file, but it
  2019. * is so far negative - it has no inode.
  2020. *
  2021. * If the create succeeds, we fill in the inode information
  2022. * with d_instantiate().
  2023. */
  2024. static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2025. bool excl)
  2026. {
  2027. handle_t *handle;
  2028. struct inode *inode;
  2029. int err, credits, retries = 0;
  2030. dquot_initialize(dir);
  2031. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2032. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2033. retry:
  2034. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2035. NULL, EXT4_HT_DIR, credits);
  2036. handle = ext4_journal_current_handle();
  2037. err = PTR_ERR(inode);
  2038. if (!IS_ERR(inode)) {
  2039. inode->i_op = &ext4_file_inode_operations;
  2040. inode->i_fop = &ext4_file_operations;
  2041. ext4_set_aops(inode);
  2042. err = ext4_add_nondir(handle, dentry, inode);
  2043. if (!err && IS_DIRSYNC(dir))
  2044. ext4_handle_sync(handle);
  2045. }
  2046. if (handle)
  2047. ext4_journal_stop(handle);
  2048. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2049. goto retry;
  2050. return err;
  2051. }
  2052. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  2053. umode_t mode, dev_t rdev)
  2054. {
  2055. handle_t *handle;
  2056. struct inode *inode;
  2057. int err, credits, retries = 0;
  2058. if (!new_valid_dev(rdev))
  2059. return -EINVAL;
  2060. dquot_initialize(dir);
  2061. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2062. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2063. retry:
  2064. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2065. NULL, EXT4_HT_DIR, credits);
  2066. handle = ext4_journal_current_handle();
  2067. err = PTR_ERR(inode);
  2068. if (!IS_ERR(inode)) {
  2069. init_special_inode(inode, inode->i_mode, rdev);
  2070. inode->i_op = &ext4_special_inode_operations;
  2071. err = ext4_add_nondir(handle, dentry, inode);
  2072. if (!err && IS_DIRSYNC(dir))
  2073. ext4_handle_sync(handle);
  2074. }
  2075. if (handle)
  2076. ext4_journal_stop(handle);
  2077. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2078. goto retry;
  2079. return err;
  2080. }
  2081. static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  2082. {
  2083. handle_t *handle;
  2084. struct inode *inode;
  2085. int err, retries = 0;
  2086. dquot_initialize(dir);
  2087. retry:
  2088. inode = ext4_new_inode_start_handle(dir, mode,
  2089. NULL, 0, NULL,
  2090. EXT4_HT_DIR,
  2091. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2092. 4 + EXT4_XATTR_TRANS_BLOCKS);
  2093. handle = ext4_journal_current_handle();
  2094. err = PTR_ERR(inode);
  2095. if (!IS_ERR(inode)) {
  2096. inode->i_op = &ext4_file_inode_operations;
  2097. inode->i_fop = &ext4_file_operations;
  2098. ext4_set_aops(inode);
  2099. d_tmpfile(dentry, inode);
  2100. err = ext4_orphan_add(handle, inode);
  2101. if (err)
  2102. goto err_unlock_inode;
  2103. mark_inode_dirty(inode);
  2104. unlock_new_inode(inode);
  2105. }
  2106. if (handle)
  2107. ext4_journal_stop(handle);
  2108. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2109. goto retry;
  2110. return err;
  2111. err_unlock_inode:
  2112. ext4_journal_stop(handle);
  2113. unlock_new_inode(inode);
  2114. return err;
  2115. }
  2116. struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
  2117. struct ext4_dir_entry_2 *de,
  2118. int blocksize, int csum_size,
  2119. unsigned int parent_ino, int dotdot_real_len)
  2120. {
  2121. de->inode = cpu_to_le32(inode->i_ino);
  2122. de->name_len = 1;
  2123. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  2124. blocksize);
  2125. strcpy(de->name, ".");
  2126. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2127. de = ext4_next_entry(de, blocksize);
  2128. de->inode = cpu_to_le32(parent_ino);
  2129. de->name_len = 2;
  2130. if (!dotdot_real_len)
  2131. de->rec_len = ext4_rec_len_to_disk(blocksize -
  2132. (csum_size + EXT4_DIR_REC_LEN(1)),
  2133. blocksize);
  2134. else
  2135. de->rec_len = ext4_rec_len_to_disk(
  2136. EXT4_DIR_REC_LEN(de->name_len), blocksize);
  2137. strcpy(de->name, "..");
  2138. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2139. return ext4_next_entry(de, blocksize);
  2140. }
  2141. static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2142. struct inode *inode)
  2143. {
  2144. struct buffer_head *dir_block = NULL;
  2145. struct ext4_dir_entry_2 *de;
  2146. struct ext4_dir_entry_tail *t;
  2147. ext4_lblk_t block = 0;
  2148. unsigned int blocksize = dir->i_sb->s_blocksize;
  2149. int csum_size = 0;
  2150. int err;
  2151. if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
  2152. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  2153. csum_size = sizeof(struct ext4_dir_entry_tail);
  2154. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2155. err = ext4_try_create_inline_dir(handle, dir, inode);
  2156. if (err < 0 && err != -ENOSPC)
  2157. goto out;
  2158. if (!err)
  2159. goto out;
  2160. }
  2161. inode->i_size = 0;
  2162. dir_block = ext4_append(handle, inode, &block);
  2163. if (IS_ERR(dir_block))
  2164. return PTR_ERR(dir_block);
  2165. BUFFER_TRACE(dir_block, "get_write_access");
  2166. err = ext4_journal_get_write_access(handle, dir_block);
  2167. if (err)
  2168. goto out;
  2169. de = (struct ext4_dir_entry_2 *)dir_block->b_data;
  2170. ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
  2171. set_nlink(inode, 2);
  2172. if (csum_size) {
  2173. t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
  2174. initialize_dirent_tail(t, blocksize);
  2175. }
  2176. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  2177. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  2178. if (err)
  2179. goto out;
  2180. set_buffer_verified(dir_block);
  2181. out:
  2182. brelse(dir_block);
  2183. return err;
  2184. }
  2185. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2186. {
  2187. handle_t *handle;
  2188. struct inode *inode;
  2189. int err, credits, retries = 0;
  2190. if (EXT4_DIR_LINK_MAX(dir))
  2191. return -EMLINK;
  2192. dquot_initialize(dir);
  2193. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2194. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2195. retry:
  2196. inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
  2197. &dentry->d_name,
  2198. 0, NULL, EXT4_HT_DIR, credits);
  2199. handle = ext4_journal_current_handle();
  2200. err = PTR_ERR(inode);
  2201. if (IS_ERR(inode))
  2202. goto out_stop;
  2203. inode->i_op = &ext4_dir_inode_operations;
  2204. inode->i_fop = &ext4_dir_operations;
  2205. err = ext4_init_new_dir(handle, dir, inode);
  2206. if (err)
  2207. goto out_clear_inode;
  2208. err = ext4_mark_inode_dirty(handle, inode);
  2209. if (!err)
  2210. err = ext4_add_entry(handle, dentry, inode);
  2211. if (err) {
  2212. out_clear_inode:
  2213. clear_nlink(inode);
  2214. unlock_new_inode(inode);
  2215. ext4_mark_inode_dirty(handle, inode);
  2216. iput(inode);
  2217. goto out_stop;
  2218. }
  2219. ext4_inc_count(handle, dir);
  2220. ext4_update_dx_flag(dir);
  2221. err = ext4_mark_inode_dirty(handle, dir);
  2222. if (err)
  2223. goto out_clear_inode;
  2224. unlock_new_inode(inode);
  2225. d_instantiate(dentry, inode);
  2226. if (IS_DIRSYNC(dir))
  2227. ext4_handle_sync(handle);
  2228. out_stop:
  2229. if (handle)
  2230. ext4_journal_stop(handle);
  2231. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2232. goto retry;
  2233. return err;
  2234. }
  2235. /*
  2236. * routine to check that the specified directory is empty (for rmdir)
  2237. */
  2238. static int empty_dir(struct inode *inode)
  2239. {
  2240. unsigned int offset;
  2241. struct buffer_head *bh;
  2242. struct ext4_dir_entry_2 *de, *de1;
  2243. struct super_block *sb;
  2244. int err = 0;
  2245. if (ext4_has_inline_data(inode)) {
  2246. int has_inline_data = 1;
  2247. err = empty_inline_dir(inode, &has_inline_data);
  2248. if (has_inline_data)
  2249. return err;
  2250. }
  2251. sb = inode->i_sb;
  2252. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
  2253. EXT4_ERROR_INODE(inode, "invalid size");
  2254. return 1;
  2255. }
  2256. bh = ext4_read_dirblock(inode, 0, EITHER);
  2257. if (IS_ERR(bh))
  2258. return 1;
  2259. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2260. de1 = ext4_next_entry(de, sb->s_blocksize);
  2261. if (le32_to_cpu(de->inode) != inode->i_ino ||
  2262. !le32_to_cpu(de1->inode) ||
  2263. strcmp(".", de->name) ||
  2264. strcmp("..", de1->name)) {
  2265. ext4_warning(inode->i_sb,
  2266. "bad directory (dir #%lu) - no `.' or `..'",
  2267. inode->i_ino);
  2268. brelse(bh);
  2269. return 1;
  2270. }
  2271. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  2272. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  2273. de = ext4_next_entry(de1, sb->s_blocksize);
  2274. while (offset < inode->i_size) {
  2275. if (!bh ||
  2276. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  2277. unsigned int lblock;
  2278. err = 0;
  2279. brelse(bh);
  2280. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  2281. bh = ext4_read_dirblock(inode, lblock, EITHER);
  2282. if (IS_ERR(bh))
  2283. return 1;
  2284. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2285. }
  2286. if (ext4_check_dir_entry(inode, NULL, de, bh,
  2287. bh->b_data, bh->b_size, offset)) {
  2288. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  2289. sb->s_blocksize);
  2290. offset = (offset | (sb->s_blocksize - 1)) + 1;
  2291. continue;
  2292. }
  2293. if (le32_to_cpu(de->inode)) {
  2294. brelse(bh);
  2295. return 0;
  2296. }
  2297. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2298. de = ext4_next_entry(de, sb->s_blocksize);
  2299. }
  2300. brelse(bh);
  2301. return 1;
  2302. }
  2303. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  2304. * such inodes, starting at the superblock, in case we crash before the
  2305. * file is closed/deleted, or in case the inode truncate spans multiple
  2306. * transactions and the last transaction is not recovered after a crash.
  2307. *
  2308. * At filesystem recovery time, we walk this list deleting unlinked
  2309. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  2310. */
  2311. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  2312. {
  2313. struct super_block *sb = inode->i_sb;
  2314. struct ext4_iloc iloc;
  2315. int err = 0, rc;
  2316. if (!EXT4_SB(sb)->s_journal)
  2317. return 0;
  2318. mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
  2319. if (!list_empty(&EXT4_I(inode)->i_orphan))
  2320. goto out_unlock;
  2321. /*
  2322. * Orphan handling is only valid for files with data blocks
  2323. * being truncated, or files being unlinked. Note that we either
  2324. * hold i_mutex, or the inode can not be referenced from outside,
  2325. * so i_nlink should not be bumped due to race
  2326. */
  2327. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  2328. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  2329. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  2330. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  2331. if (err)
  2332. goto out_unlock;
  2333. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2334. if (err)
  2335. goto out_unlock;
  2336. /*
  2337. * Due to previous errors inode may be already a part of on-disk
  2338. * orphan list. If so skip on-disk list modification.
  2339. */
  2340. if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
  2341. (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
  2342. goto mem_insert;
  2343. /* Insert this inode at the head of the on-disk orphan list... */
  2344. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  2345. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  2346. err = ext4_handle_dirty_super(handle, sb);
  2347. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2348. if (!err)
  2349. err = rc;
  2350. /* Only add to the head of the in-memory list if all the
  2351. * previous operations succeeded. If the orphan_add is going to
  2352. * fail (possibly taking the journal offline), we can't risk
  2353. * leaving the inode on the orphan list: stray orphan-list
  2354. * entries can cause panics at unmount time.
  2355. *
  2356. * This is safe: on error we're going to ignore the orphan list
  2357. * anyway on the next recovery. */
  2358. mem_insert:
  2359. if (!err)
  2360. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  2361. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  2362. jbd_debug(4, "orphan inode %lu will point to %d\n",
  2363. inode->i_ino, NEXT_ORPHAN(inode));
  2364. out_unlock:
  2365. mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
  2366. ext4_std_error(inode->i_sb, err);
  2367. return err;
  2368. }
  2369. /*
  2370. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  2371. * of such inodes stored on disk, because it is finally being cleaned up.
  2372. */
  2373. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  2374. {
  2375. struct list_head *prev;
  2376. struct ext4_inode_info *ei = EXT4_I(inode);
  2377. struct ext4_sb_info *sbi;
  2378. __u32 ino_next;
  2379. struct ext4_iloc iloc;
  2380. int err = 0;
  2381. if ((!EXT4_SB(inode->i_sb)->s_journal) &&
  2382. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS))
  2383. return 0;
  2384. mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  2385. if (list_empty(&ei->i_orphan))
  2386. goto out;
  2387. ino_next = NEXT_ORPHAN(inode);
  2388. prev = ei->i_orphan.prev;
  2389. sbi = EXT4_SB(inode->i_sb);
  2390. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  2391. list_del_init(&ei->i_orphan);
  2392. /* If we're on an error path, we may not have a valid
  2393. * transaction handle with which to update the orphan list on
  2394. * disk, but we still need to remove the inode from the linked
  2395. * list in memory. */
  2396. if (!handle)
  2397. goto out;
  2398. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2399. if (err)
  2400. goto out_err;
  2401. if (prev == &sbi->s_orphan) {
  2402. jbd_debug(4, "superblock will point to %u\n", ino_next);
  2403. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2404. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2405. if (err)
  2406. goto out_brelse;
  2407. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  2408. err = ext4_handle_dirty_super(handle, inode->i_sb);
  2409. } else {
  2410. struct ext4_iloc iloc2;
  2411. struct inode *i_prev =
  2412. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  2413. jbd_debug(4, "orphan inode %lu will point to %u\n",
  2414. i_prev->i_ino, ino_next);
  2415. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  2416. if (err)
  2417. goto out_brelse;
  2418. NEXT_ORPHAN(i_prev) = ino_next;
  2419. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  2420. }
  2421. if (err)
  2422. goto out_brelse;
  2423. NEXT_ORPHAN(inode) = 0;
  2424. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2425. out_err:
  2426. ext4_std_error(inode->i_sb, err);
  2427. out:
  2428. mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
  2429. return err;
  2430. out_brelse:
  2431. brelse(iloc.bh);
  2432. goto out_err;
  2433. }
  2434. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  2435. {
  2436. int retval;
  2437. struct inode *inode;
  2438. struct buffer_head *bh;
  2439. struct ext4_dir_entry_2 *de;
  2440. handle_t *handle = NULL;
  2441. /* Initialize quotas before so that eventual writes go in
  2442. * separate transaction */
  2443. dquot_initialize(dir);
  2444. dquot_initialize(dentry->d_inode);
  2445. retval = -ENOENT;
  2446. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2447. if (!bh)
  2448. goto end_rmdir;
  2449. inode = dentry->d_inode;
  2450. retval = -EIO;
  2451. if (le32_to_cpu(de->inode) != inode->i_ino)
  2452. goto end_rmdir;
  2453. retval = -ENOTEMPTY;
  2454. if (!empty_dir(inode))
  2455. goto end_rmdir;
  2456. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2457. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2458. if (IS_ERR(handle)) {
  2459. retval = PTR_ERR(handle);
  2460. handle = NULL;
  2461. goto end_rmdir;
  2462. }
  2463. if (IS_DIRSYNC(dir))
  2464. ext4_handle_sync(handle);
  2465. retval = ext4_delete_entry(handle, dir, de, bh);
  2466. if (retval)
  2467. goto end_rmdir;
  2468. if (!EXT4_DIR_LINK_EMPTY(inode))
  2469. ext4_warning(inode->i_sb,
  2470. "empty directory has too many links (%d)",
  2471. inode->i_nlink);
  2472. inode->i_version++;
  2473. clear_nlink(inode);
  2474. /* There's no need to set i_disksize: the fact that i_nlink is
  2475. * zero will ensure that the right thing happens during any
  2476. * recovery. */
  2477. inode->i_size = 0;
  2478. ext4_orphan_add(handle, inode);
  2479. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  2480. ext4_mark_inode_dirty(handle, inode);
  2481. ext4_dec_count(handle, dir);
  2482. ext4_update_dx_flag(dir);
  2483. ext4_mark_inode_dirty(handle, dir);
  2484. end_rmdir:
  2485. brelse(bh);
  2486. if (handle)
  2487. ext4_journal_stop(handle);
  2488. return retval;
  2489. }
  2490. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2491. {
  2492. int retval;
  2493. struct inode *inode;
  2494. struct buffer_head *bh;
  2495. struct ext4_dir_entry_2 *de;
  2496. handle_t *handle = NULL;
  2497. trace_ext4_unlink_enter(dir, dentry);
  2498. /* Initialize quotas before so that eventual writes go
  2499. * in separate transaction */
  2500. dquot_initialize(dir);
  2501. dquot_initialize(dentry->d_inode);
  2502. retval = -ENOENT;
  2503. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2504. if (!bh)
  2505. goto end_unlink;
  2506. inode = dentry->d_inode;
  2507. retval = -EIO;
  2508. if (le32_to_cpu(de->inode) != inode->i_ino)
  2509. goto end_unlink;
  2510. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2511. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2512. if (IS_ERR(handle)) {
  2513. retval = PTR_ERR(handle);
  2514. handle = NULL;
  2515. goto end_unlink;
  2516. }
  2517. if (IS_DIRSYNC(dir))
  2518. ext4_handle_sync(handle);
  2519. if (!inode->i_nlink) {
  2520. ext4_warning(inode->i_sb,
  2521. "Deleting nonexistent file (%lu), %d",
  2522. inode->i_ino, inode->i_nlink);
  2523. set_nlink(inode, 1);
  2524. }
  2525. retval = ext4_delete_entry(handle, dir, de, bh);
  2526. if (retval)
  2527. goto end_unlink;
  2528. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  2529. ext4_update_dx_flag(dir);
  2530. ext4_mark_inode_dirty(handle, dir);
  2531. drop_nlink(inode);
  2532. if (!inode->i_nlink)
  2533. ext4_orphan_add(handle, inode);
  2534. inode->i_ctime = ext4_current_time(inode);
  2535. ext4_mark_inode_dirty(handle, inode);
  2536. retval = 0;
  2537. end_unlink:
  2538. brelse(bh);
  2539. if (handle)
  2540. ext4_journal_stop(handle);
  2541. trace_ext4_unlink_exit(dentry, retval);
  2542. return retval;
  2543. }
  2544. static int ext4_symlink(struct inode *dir,
  2545. struct dentry *dentry, const char *symname)
  2546. {
  2547. handle_t *handle;
  2548. struct inode *inode;
  2549. int l, err, retries = 0;
  2550. int credits;
  2551. l = strlen(symname)+1;
  2552. if (l > dir->i_sb->s_blocksize)
  2553. return -ENAMETOOLONG;
  2554. dquot_initialize(dir);
  2555. if (l > EXT4_N_BLOCKS * 4) {
  2556. /*
  2557. * For non-fast symlinks, we just allocate inode and put it on
  2558. * orphan list in the first transaction => we need bitmap,
  2559. * group descriptor, sb, inode block, quota blocks, and
  2560. * possibly selinux xattr blocks.
  2561. */
  2562. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2563. EXT4_XATTR_TRANS_BLOCKS;
  2564. } else {
  2565. /*
  2566. * Fast symlink. We have to add entry to directory
  2567. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2568. * allocate new inode (bitmap, group descriptor, inode block,
  2569. * quota blocks, sb is already counted in previous macros).
  2570. */
  2571. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2572. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
  2573. }
  2574. retry:
  2575. inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
  2576. &dentry->d_name, 0, NULL,
  2577. EXT4_HT_DIR, credits);
  2578. handle = ext4_journal_current_handle();
  2579. err = PTR_ERR(inode);
  2580. if (IS_ERR(inode))
  2581. goto out_stop;
  2582. if (l > EXT4_N_BLOCKS * 4) {
  2583. inode->i_op = &ext4_symlink_inode_operations;
  2584. ext4_set_aops(inode);
  2585. /*
  2586. * We cannot call page_symlink() with transaction started
  2587. * because it calls into ext4_write_begin() which can wait
  2588. * for transaction commit if we are running out of space
  2589. * and thus we deadlock. So we have to stop transaction now
  2590. * and restart it when symlink contents is written.
  2591. *
  2592. * To keep fs consistent in case of crash, we have to put inode
  2593. * to orphan list in the mean time.
  2594. */
  2595. drop_nlink(inode);
  2596. err = ext4_orphan_add(handle, inode);
  2597. ext4_journal_stop(handle);
  2598. if (err)
  2599. goto err_drop_inode;
  2600. err = __page_symlink(inode, symname, l, 1);
  2601. if (err)
  2602. goto err_drop_inode;
  2603. /*
  2604. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2605. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2606. */
  2607. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2608. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2609. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2610. if (IS_ERR(handle)) {
  2611. err = PTR_ERR(handle);
  2612. goto err_drop_inode;
  2613. }
  2614. set_nlink(inode, 1);
  2615. err = ext4_orphan_del(handle, inode);
  2616. if (err) {
  2617. ext4_journal_stop(handle);
  2618. clear_nlink(inode);
  2619. goto err_drop_inode;
  2620. }
  2621. } else {
  2622. /* clear the extent format for fast symlink */
  2623. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2624. inode->i_op = &ext4_fast_symlink_inode_operations;
  2625. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2626. inode->i_size = l-1;
  2627. }
  2628. EXT4_I(inode)->i_disksize = inode->i_size;
  2629. err = ext4_add_nondir(handle, dentry, inode);
  2630. if (!err && IS_DIRSYNC(dir))
  2631. ext4_handle_sync(handle);
  2632. out_stop:
  2633. if (handle)
  2634. ext4_journal_stop(handle);
  2635. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2636. goto retry;
  2637. return err;
  2638. err_drop_inode:
  2639. unlock_new_inode(inode);
  2640. iput(inode);
  2641. return err;
  2642. }
  2643. static int ext4_link(struct dentry *old_dentry,
  2644. struct inode *dir, struct dentry *dentry)
  2645. {
  2646. handle_t *handle;
  2647. struct inode *inode = old_dentry->d_inode;
  2648. int err, retries = 0;
  2649. if (inode->i_nlink >= EXT4_LINK_MAX)
  2650. return -EMLINK;
  2651. dquot_initialize(dir);
  2652. retry:
  2653. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2654. (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2655. EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
  2656. if (IS_ERR(handle))
  2657. return PTR_ERR(handle);
  2658. if (IS_DIRSYNC(dir))
  2659. ext4_handle_sync(handle);
  2660. inode->i_ctime = ext4_current_time(inode);
  2661. ext4_inc_count(handle, inode);
  2662. ihold(inode);
  2663. err = ext4_add_entry(handle, dentry, inode);
  2664. if (!err) {
  2665. ext4_mark_inode_dirty(handle, inode);
  2666. /* this can happen only for tmpfile being
  2667. * linked the first time
  2668. */
  2669. if (inode->i_nlink == 1)
  2670. ext4_orphan_del(handle, inode);
  2671. d_instantiate(dentry, inode);
  2672. } else {
  2673. drop_nlink(inode);
  2674. iput(inode);
  2675. }
  2676. ext4_journal_stop(handle);
  2677. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2678. goto retry;
  2679. return err;
  2680. }
  2681. /*
  2682. * Try to find buffer head where contains the parent block.
  2683. * It should be the inode block if it is inlined or the 1st block
  2684. * if it is a normal dir.
  2685. */
  2686. static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
  2687. struct inode *inode,
  2688. int *retval,
  2689. struct ext4_dir_entry_2 **parent_de,
  2690. int *inlined)
  2691. {
  2692. struct buffer_head *bh;
  2693. if (!ext4_has_inline_data(inode)) {
  2694. bh = ext4_read_dirblock(inode, 0, EITHER);
  2695. if (IS_ERR(bh)) {
  2696. *retval = PTR_ERR(bh);
  2697. return NULL;
  2698. }
  2699. *parent_de = ext4_next_entry(
  2700. (struct ext4_dir_entry_2 *)bh->b_data,
  2701. inode->i_sb->s_blocksize);
  2702. return bh;
  2703. }
  2704. *inlined = 1;
  2705. return ext4_get_first_inline_block(inode, parent_de, retval);
  2706. }
  2707. struct ext4_renament {
  2708. struct inode *dir;
  2709. struct dentry *dentry;
  2710. struct inode *inode;
  2711. bool is_dir;
  2712. int dir_nlink_delta;
  2713. /* entry for "dentry" */
  2714. struct buffer_head *bh;
  2715. struct ext4_dir_entry_2 *de;
  2716. int inlined;
  2717. /* entry for ".." in inode if it's a directory */
  2718. struct buffer_head *dir_bh;
  2719. struct ext4_dir_entry_2 *parent_de;
  2720. int dir_inlined;
  2721. };
  2722. static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
  2723. {
  2724. int retval;
  2725. ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
  2726. &retval, &ent->parent_de,
  2727. &ent->dir_inlined);
  2728. if (!ent->dir_bh)
  2729. return retval;
  2730. if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
  2731. return -EIO;
  2732. BUFFER_TRACE(ent->dir_bh, "get_write_access");
  2733. return ext4_journal_get_write_access(handle, ent->dir_bh);
  2734. }
  2735. static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
  2736. unsigned dir_ino)
  2737. {
  2738. int retval;
  2739. ent->parent_de->inode = cpu_to_le32(dir_ino);
  2740. BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
  2741. if (!ent->dir_inlined) {
  2742. if (is_dx(ent->inode)) {
  2743. retval = ext4_handle_dirty_dx_node(handle,
  2744. ent->inode,
  2745. ent->dir_bh);
  2746. } else {
  2747. retval = ext4_handle_dirty_dirent_node(handle,
  2748. ent->inode,
  2749. ent->dir_bh);
  2750. }
  2751. } else {
  2752. retval = ext4_mark_inode_dirty(handle, ent->inode);
  2753. }
  2754. if (retval) {
  2755. ext4_std_error(ent->dir->i_sb, retval);
  2756. return retval;
  2757. }
  2758. return 0;
  2759. }
  2760. static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
  2761. unsigned ino, unsigned file_type)
  2762. {
  2763. int retval;
  2764. BUFFER_TRACE(ent->bh, "get write access");
  2765. retval = ext4_journal_get_write_access(handle, ent->bh);
  2766. if (retval)
  2767. return retval;
  2768. ent->de->inode = cpu_to_le32(ino);
  2769. if (EXT4_HAS_INCOMPAT_FEATURE(ent->dir->i_sb,
  2770. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2771. ent->de->file_type = file_type;
  2772. ent->dir->i_version++;
  2773. ent->dir->i_ctime = ent->dir->i_mtime =
  2774. ext4_current_time(ent->dir);
  2775. ext4_mark_inode_dirty(handle, ent->dir);
  2776. BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
  2777. if (!ent->inlined) {
  2778. retval = ext4_handle_dirty_dirent_node(handle,
  2779. ent->dir, ent->bh);
  2780. if (unlikely(retval)) {
  2781. ext4_std_error(ent->dir->i_sb, retval);
  2782. return retval;
  2783. }
  2784. }
  2785. brelse(ent->bh);
  2786. ent->bh = NULL;
  2787. return 0;
  2788. }
  2789. static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
  2790. const struct qstr *d_name)
  2791. {
  2792. int retval = -ENOENT;
  2793. struct buffer_head *bh;
  2794. struct ext4_dir_entry_2 *de;
  2795. bh = ext4_find_entry(dir, d_name, &de, NULL);
  2796. if (bh) {
  2797. retval = ext4_delete_entry(handle, dir, de, bh);
  2798. brelse(bh);
  2799. }
  2800. return retval;
  2801. }
  2802. static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent)
  2803. {
  2804. int retval;
  2805. /*
  2806. * ent->de could have moved from under us during htree split, so make
  2807. * sure that we are deleting the right entry. We might also be pointing
  2808. * to a stale entry in the unused part of ent->bh so just checking inum
  2809. * and the name isn't enough.
  2810. */
  2811. if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
  2812. ent->de->name_len != ent->dentry->d_name.len ||
  2813. strncmp(ent->de->name, ent->dentry->d_name.name,
  2814. ent->de->name_len)) {
  2815. retval = ext4_find_delete_entry(handle, ent->dir,
  2816. &ent->dentry->d_name);
  2817. } else {
  2818. retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
  2819. if (retval == -ENOENT) {
  2820. retval = ext4_find_delete_entry(handle, ent->dir,
  2821. &ent->dentry->d_name);
  2822. }
  2823. }
  2824. if (retval) {
  2825. ext4_warning(ent->dir->i_sb,
  2826. "Deleting old file (%lu), %d, error=%d",
  2827. ent->dir->i_ino, ent->dir->i_nlink, retval);
  2828. }
  2829. }
  2830. static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
  2831. {
  2832. if (ent->dir_nlink_delta) {
  2833. if (ent->dir_nlink_delta == -1)
  2834. ext4_dec_count(handle, ent->dir);
  2835. else
  2836. ext4_inc_count(handle, ent->dir);
  2837. ext4_mark_inode_dirty(handle, ent->dir);
  2838. }
  2839. }
  2840. /*
  2841. * Anybody can rename anything with this: the permission checks are left to the
  2842. * higher-level routines.
  2843. *
  2844. * n.b. old_{dentry,inode) refers to the source dentry/inode
  2845. * while new_{dentry,inode) refers to the destination dentry/inode
  2846. * This comes from rename(const char *oldpath, const char *newpath)
  2847. */
  2848. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2849. struct inode *new_dir, struct dentry *new_dentry)
  2850. {
  2851. handle_t *handle = NULL;
  2852. struct ext4_renament old = {
  2853. .dir = old_dir,
  2854. .dentry = old_dentry,
  2855. .inode = old_dentry->d_inode,
  2856. };
  2857. struct ext4_renament new = {
  2858. .dir = new_dir,
  2859. .dentry = new_dentry,
  2860. .inode = new_dentry->d_inode,
  2861. };
  2862. int retval;
  2863. dquot_initialize(old.dir);
  2864. dquot_initialize(new.dir);
  2865. /* Initialize quotas before so that eventual writes go
  2866. * in separate transaction */
  2867. if (new.inode)
  2868. dquot_initialize(new.inode);
  2869. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
  2870. /*
  2871. * Check for inode number is _not_ due to possible IO errors.
  2872. * We might rmdir the source, keep it as pwd of some process
  2873. * and merrily kill the link to whatever was created under the
  2874. * same name. Goodbye sticky bit ;-<
  2875. */
  2876. retval = -ENOENT;
  2877. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  2878. goto end_rename;
  2879. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  2880. &new.de, &new.inlined);
  2881. if (new.bh) {
  2882. if (!new.inode) {
  2883. brelse(new.bh);
  2884. new.bh = NULL;
  2885. }
  2886. }
  2887. if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
  2888. ext4_alloc_da_blocks(old.inode);
  2889. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  2890. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  2891. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  2892. if (IS_ERR(handle))
  2893. return PTR_ERR(handle);
  2894. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  2895. ext4_handle_sync(handle);
  2896. if (S_ISDIR(old.inode->i_mode)) {
  2897. if (new.inode) {
  2898. retval = -ENOTEMPTY;
  2899. if (!empty_dir(new.inode))
  2900. goto end_rename;
  2901. } else {
  2902. retval = -EMLINK;
  2903. if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
  2904. goto end_rename;
  2905. }
  2906. retval = ext4_rename_dir_prepare(handle, &old);
  2907. if (retval)
  2908. goto end_rename;
  2909. }
  2910. if (!new.bh) {
  2911. retval = ext4_add_entry(handle, new.dentry, old.inode);
  2912. if (retval)
  2913. goto end_rename;
  2914. } else {
  2915. retval = ext4_setent(handle, &new,
  2916. old.inode->i_ino, old.de->file_type);
  2917. if (retval)
  2918. goto end_rename;
  2919. }
  2920. /*
  2921. * Like most other Unix systems, set the ctime for inodes on a
  2922. * rename.
  2923. */
  2924. old.inode->i_ctime = ext4_current_time(old.inode);
  2925. ext4_mark_inode_dirty(handle, old.inode);
  2926. /*
  2927. * ok, that's it
  2928. */
  2929. ext4_rename_delete(handle, &old);
  2930. if (new.inode) {
  2931. ext4_dec_count(handle, new.inode);
  2932. new.inode->i_ctime = ext4_current_time(new.inode);
  2933. }
  2934. old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
  2935. ext4_update_dx_flag(old.dir);
  2936. if (old.dir_bh) {
  2937. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  2938. if (retval)
  2939. goto end_rename;
  2940. ext4_dec_count(handle, old.dir);
  2941. if (new.inode) {
  2942. /* checked empty_dir above, can't have another parent,
  2943. * ext4_dec_count() won't work for many-linked dirs */
  2944. clear_nlink(new.inode);
  2945. } else {
  2946. ext4_inc_count(handle, new.dir);
  2947. ext4_update_dx_flag(new.dir);
  2948. ext4_mark_inode_dirty(handle, new.dir);
  2949. }
  2950. }
  2951. ext4_mark_inode_dirty(handle, old.dir);
  2952. if (new.inode) {
  2953. ext4_mark_inode_dirty(handle, new.inode);
  2954. if (!new.inode->i_nlink)
  2955. ext4_orphan_add(handle, new.inode);
  2956. }
  2957. retval = 0;
  2958. end_rename:
  2959. brelse(old.dir_bh);
  2960. brelse(old.bh);
  2961. brelse(new.bh);
  2962. if (handle)
  2963. ext4_journal_stop(handle);
  2964. return retval;
  2965. }
  2966. static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  2967. struct inode *new_dir, struct dentry *new_dentry)
  2968. {
  2969. handle_t *handle = NULL;
  2970. struct ext4_renament old = {
  2971. .dir = old_dir,
  2972. .dentry = old_dentry,
  2973. .inode = old_dentry->d_inode,
  2974. };
  2975. struct ext4_renament new = {
  2976. .dir = new_dir,
  2977. .dentry = new_dentry,
  2978. .inode = new_dentry->d_inode,
  2979. };
  2980. u8 new_file_type;
  2981. int retval;
  2982. dquot_initialize(old.dir);
  2983. dquot_initialize(new.dir);
  2984. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
  2985. &old.de, &old.inlined);
  2986. /*
  2987. * Check for inode number is _not_ due to possible IO errors.
  2988. * We might rmdir the source, keep it as pwd of some process
  2989. * and merrily kill the link to whatever was created under the
  2990. * same name. Goodbye sticky bit ;-<
  2991. */
  2992. retval = -ENOENT;
  2993. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  2994. goto end_rename;
  2995. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  2996. &new.de, &new.inlined);
  2997. /* RENAME_EXCHANGE case: old *and* new must both exist */
  2998. if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
  2999. goto end_rename;
  3000. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  3001. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3002. 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  3003. if (IS_ERR(handle))
  3004. return PTR_ERR(handle);
  3005. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3006. ext4_handle_sync(handle);
  3007. if (S_ISDIR(old.inode->i_mode)) {
  3008. old.is_dir = true;
  3009. retval = ext4_rename_dir_prepare(handle, &old);
  3010. if (retval)
  3011. goto end_rename;
  3012. }
  3013. if (S_ISDIR(new.inode->i_mode)) {
  3014. new.is_dir = true;
  3015. retval = ext4_rename_dir_prepare(handle, &new);
  3016. if (retval)
  3017. goto end_rename;
  3018. }
  3019. /*
  3020. * Other than the special case of overwriting a directory, parents'
  3021. * nlink only needs to be modified if this is a cross directory rename.
  3022. */
  3023. if (old.dir != new.dir && old.is_dir != new.is_dir) {
  3024. old.dir_nlink_delta = old.is_dir ? -1 : 1;
  3025. new.dir_nlink_delta = -old.dir_nlink_delta;
  3026. retval = -EMLINK;
  3027. if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
  3028. (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
  3029. goto end_rename;
  3030. }
  3031. new_file_type = new.de->file_type;
  3032. retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
  3033. if (retval)
  3034. goto end_rename;
  3035. retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
  3036. if (retval)
  3037. goto end_rename;
  3038. /*
  3039. * Like most other Unix systems, set the ctime for inodes on a
  3040. * rename.
  3041. */
  3042. old.inode->i_ctime = ext4_current_time(old.inode);
  3043. new.inode->i_ctime = ext4_current_time(new.inode);
  3044. ext4_mark_inode_dirty(handle, old.inode);
  3045. ext4_mark_inode_dirty(handle, new.inode);
  3046. if (old.dir_bh) {
  3047. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3048. if (retval)
  3049. goto end_rename;
  3050. }
  3051. if (new.dir_bh) {
  3052. retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
  3053. if (retval)
  3054. goto end_rename;
  3055. }
  3056. ext4_update_dir_count(handle, &old);
  3057. ext4_update_dir_count(handle, &new);
  3058. retval = 0;
  3059. end_rename:
  3060. brelse(old.dir_bh);
  3061. brelse(new.dir_bh);
  3062. brelse(old.bh);
  3063. brelse(new.bh);
  3064. if (handle)
  3065. ext4_journal_stop(handle);
  3066. return retval;
  3067. }
  3068. static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
  3069. struct inode *new_dir, struct dentry *new_dentry,
  3070. unsigned int flags)
  3071. {
  3072. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  3073. return -EINVAL;
  3074. if (flags & RENAME_EXCHANGE) {
  3075. return ext4_cross_rename(old_dir, old_dentry,
  3076. new_dir, new_dentry);
  3077. }
  3078. /*
  3079. * Existence checking was done by the VFS, otherwise "RENAME_NOREPLACE"
  3080. * is equivalent to regular rename.
  3081. */
  3082. return ext4_rename(old_dir, old_dentry, new_dir, new_dentry);
  3083. }
  3084. /*
  3085. * directories can handle most operations...
  3086. */
  3087. const struct inode_operations ext4_dir_inode_operations = {
  3088. .create = ext4_create,
  3089. .lookup = ext4_lookup,
  3090. .link = ext4_link,
  3091. .unlink = ext4_unlink,
  3092. .symlink = ext4_symlink,
  3093. .mkdir = ext4_mkdir,
  3094. .rmdir = ext4_rmdir,
  3095. .mknod = ext4_mknod,
  3096. .tmpfile = ext4_tmpfile,
  3097. .rename = ext4_rename,
  3098. .rename2 = ext4_rename2,
  3099. .setattr = ext4_setattr,
  3100. .setxattr = generic_setxattr,
  3101. .getxattr = generic_getxattr,
  3102. .listxattr = ext4_listxattr,
  3103. .removexattr = generic_removexattr,
  3104. .get_acl = ext4_get_acl,
  3105. .set_acl = ext4_set_acl,
  3106. .fiemap = ext4_fiemap,
  3107. };
  3108. const struct inode_operations ext4_special_inode_operations = {
  3109. .setattr = ext4_setattr,
  3110. .setxattr = generic_setxattr,
  3111. .getxattr = generic_getxattr,
  3112. .listxattr = ext4_listxattr,
  3113. .removexattr = generic_removexattr,
  3114. .get_acl = ext4_get_acl,
  3115. .set_acl = ext4_set_acl,
  3116. };