namei.c 102 KB

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