| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- *
- * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
- *
- */
- #include <linux/blkdev.h>
- #include <linux/buffer_head.h>
- #include <linux/fs.h>
- #include <linux/kernel.h>
- #include <linux/nls.h>
- #include "debug.h"
- #include "ntfs.h"
- #include "ntfs_fs.h"
- // clang-format off
- const struct cpu_str NAME_MFT = {
- 4, 0, { '$', 'M', 'F', 'T' },
- };
- const struct cpu_str NAME_MIRROR = {
- 8, 0, { '$', 'M', 'F', 'T', 'M', 'i', 'r', 'r' },
- };
- const struct cpu_str NAME_LOGFILE = {
- 8, 0, { '$', 'L', 'o', 'g', 'F', 'i', 'l', 'e' },
- };
- const struct cpu_str NAME_VOLUME = {
- 7, 0, { '$', 'V', 'o', 'l', 'u', 'm', 'e' },
- };
- const struct cpu_str NAME_ATTRDEF = {
- 8, 0, { '$', 'A', 't', 't', 'r', 'D', 'e', 'f' },
- };
- const struct cpu_str NAME_ROOT = {
- 1, 0, { '.' },
- };
- const struct cpu_str NAME_BITMAP = {
- 7, 0, { '$', 'B', 'i', 't', 'm', 'a', 'p' },
- };
- const struct cpu_str NAME_BOOT = {
- 5, 0, { '$', 'B', 'o', 'o', 't' },
- };
- const struct cpu_str NAME_BADCLUS = {
- 8, 0, { '$', 'B', 'a', 'd', 'C', 'l', 'u', 's' },
- };
- const struct cpu_str NAME_QUOTA = {
- 6, 0, { '$', 'Q', 'u', 'o', 't', 'a' },
- };
- const struct cpu_str NAME_SECURE = {
- 7, 0, { '$', 'S', 'e', 'c', 'u', 'r', 'e' },
- };
- const struct cpu_str NAME_UPCASE = {
- 7, 0, { '$', 'U', 'p', 'C', 'a', 's', 'e' },
- };
- const struct cpu_str NAME_EXTEND = {
- 7, 0, { '$', 'E', 'x', 't', 'e', 'n', 'd' },
- };
- const struct cpu_str NAME_OBJID = {
- 6, 0, { '$', 'O', 'b', 'j', 'I', 'd' },
- };
- const struct cpu_str NAME_REPARSE = {
- 8, 0, { '$', 'R', 'e', 'p', 'a', 'r', 's', 'e' },
- };
- const struct cpu_str NAME_USNJRNL = {
- 8, 0, { '$', 'U', 's', 'n', 'J', 'r', 'n', 'l' },
- };
- const __le16 BAD_NAME[4] = {
- cpu_to_le16('$'), cpu_to_le16('B'), cpu_to_le16('a'), cpu_to_le16('d'),
- };
- const __le16 I30_NAME[4] = {
- cpu_to_le16('$'), cpu_to_le16('I'), cpu_to_le16('3'), cpu_to_le16('0'),
- };
- const __le16 SII_NAME[4] = {
- cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('I'), cpu_to_le16('I'),
- };
- const __le16 SDH_NAME[4] = {
- cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('D'), cpu_to_le16('H'),
- };
- const __le16 SDS_NAME[4] = {
- cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('D'), cpu_to_le16('S'),
- };
- const __le16 SO_NAME[2] = {
- cpu_to_le16('$'), cpu_to_le16('O'),
- };
- const __le16 SQ_NAME[2] = {
- cpu_to_le16('$'), cpu_to_le16('Q'),
- };
- const __le16 SR_NAME[2] = {
- cpu_to_le16('$'), cpu_to_le16('R'),
- };
- #ifdef CONFIG_NTFS3_LZX_XPRESS
- const __le16 WOF_NAME[17] = {
- cpu_to_le16('W'), cpu_to_le16('o'), cpu_to_le16('f'), cpu_to_le16('C'),
- cpu_to_le16('o'), cpu_to_le16('m'), cpu_to_le16('p'), cpu_to_le16('r'),
- cpu_to_le16('e'), cpu_to_le16('s'), cpu_to_le16('s'), cpu_to_le16('e'),
- cpu_to_le16('d'), cpu_to_le16('D'), cpu_to_le16('a'), cpu_to_le16('t'),
- cpu_to_le16('a'),
- };
- #endif
- static const __le16 CON_NAME[3] = {
- cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('N'),
- };
- static const __le16 NUL_NAME[3] = {
- cpu_to_le16('N'), cpu_to_le16('U'), cpu_to_le16('L'),
- };
- static const __le16 AUX_NAME[3] = {
- cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X'),
- };
- static const __le16 PRN_NAME[3] = {
- cpu_to_le16('P'), cpu_to_le16('R'), cpu_to_le16('N'),
- };
- static const __le16 COM_NAME[3] = {
- cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('M'),
- };
- static const __le16 LPT_NAME[3] = {
- cpu_to_le16('L'), cpu_to_le16('P'), cpu_to_le16('T'),
- };
- // clang-format on
- /*
- * ntfs_fix_pre_write - Insert fixups into @rhdr before writing to disk.
- */
- bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes)
- {
- u16 *fixup, *ptr;
- u16 sample;
- u16 fo = le16_to_cpu(rhdr->fix_off);
- u16 fn = le16_to_cpu(rhdr->fix_num);
- if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
- fn * SECTOR_SIZE > bytes) {
- return false;
- }
- /* Get fixup pointer. */
- fixup = Add2Ptr(rhdr, fo);
- if (*fixup >= 0x7FFF)
- *fixup = 1;
- else
- *fixup += 1;
- sample = *fixup;
- ptr = Add2Ptr(rhdr, SECTOR_SIZE - sizeof(short));
- while (fn--) {
- *++fixup = *ptr;
- *ptr = sample;
- ptr += SECTOR_SIZE / sizeof(short);
- }
- return true;
- }
- /*
- * ntfs_fix_post_read - Remove fixups after reading from disk.
- *
- * Return: < 0 if error, 0 if ok, 1 if need to update fixups.
- */
- int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
- bool simple)
- {
- int ret;
- u16 *fixup, *ptr;
- u16 sample, fo, fn;
- fo = le16_to_cpu(rhdr->fix_off);
- fn = simple ? ((bytes >> SECTOR_SHIFT) + 1) :
- le16_to_cpu(rhdr->fix_num);
- /* Check errors. */
- if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
- fn * SECTOR_SIZE > bytes) {
- return -E_NTFS_CORRUPT;
- }
- /* Get fixup pointer. */
- fixup = Add2Ptr(rhdr, fo);
- sample = *fixup;
- ptr = Add2Ptr(rhdr, SECTOR_SIZE - sizeof(short));
- ret = 0;
- while (fn--) {
- /* Test current word. */
- if (*ptr != sample) {
- /* Fixup does not match! Is it serious error? */
- ret = -E_NTFS_FIXUP;
- }
- /* Replace fixup. */
- *ptr = *++fixup;
- ptr += SECTOR_SIZE / sizeof(short);
- }
- return ret;
- }
- /*
- * ntfs_extend_init - Load $Extend file.
- */
- int ntfs_extend_init(struct ntfs_sb_info *sbi)
- {
- int err;
- struct super_block *sb = sbi->sb;
- struct inode *inode, *inode2;
- struct MFT_REF ref;
- if (sbi->volume.major_ver < 3) {
- ntfs_notice(sb, "Skip $Extend 'cause NTFS version");
- return 0;
- }
- ref.low = cpu_to_le32(MFT_REC_EXTEND);
- ref.high = 0;
- ref.seq = cpu_to_le16(MFT_REC_EXTEND);
- inode = ntfs_iget5(sb, &ref, &NAME_EXTEND);
- if (IS_ERR(inode)) {
- err = PTR_ERR(inode);
- ntfs_err(sb, "Failed to load $Extend (%d).", err);
- inode = NULL;
- goto out;
- }
- /* If ntfs_iget5() reads from disk it never returns bad inode. */
- if (!S_ISDIR(inode->i_mode)) {
- err = -EINVAL;
- goto out;
- }
- /* Try to find $ObjId */
- inode2 = dir_search_u(inode, &NAME_OBJID, NULL);
- if (inode2 && !IS_ERR(inode2)) {
- if (is_bad_inode(inode2)) {
- iput(inode2);
- } else {
- sbi->objid.ni = ntfs_i(inode2);
- sbi->objid_no = inode2->i_ino;
- }
- }
- /* Try to find $Quota */
- inode2 = dir_search_u(inode, &NAME_QUOTA, NULL);
- if (inode2 && !IS_ERR(inode2)) {
- sbi->quota_no = inode2->i_ino;
- iput(inode2);
- }
- /* Try to find $Reparse */
- inode2 = dir_search_u(inode, &NAME_REPARSE, NULL);
- if (inode2 && !IS_ERR(inode2)) {
- sbi->reparse.ni = ntfs_i(inode2);
- sbi->reparse_no = inode2->i_ino;
- }
- /* Try to find $UsnJrnl */
- inode2 = dir_search_u(inode, &NAME_USNJRNL, NULL);
- if (inode2 && !IS_ERR(inode2)) {
- sbi->usn_jrnl_no = inode2->i_ino;
- iput(inode2);
- }
- err = 0;
- out:
- iput(inode);
- return err;
- }
- int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi)
- {
- int err = 0;
- struct super_block *sb = sbi->sb;
- bool initialized = false;
- struct MFT_REF ref;
- struct inode *inode;
- /* Check for 4GB. */
- if (ni->vfs_inode.i_size >= 0x100000000ull) {
- ntfs_err(sb, "\x24LogFile is large than 4G.");
- err = -EINVAL;
- goto out;
- }
- sbi->flags |= NTFS_FLAGS_LOG_REPLAYING;
- ref.low = cpu_to_le32(MFT_REC_MFT);
- ref.high = 0;
- ref.seq = cpu_to_le16(1);
- inode = ntfs_iget5(sb, &ref, NULL);
- if (IS_ERR(inode))
- inode = NULL;
- if (!inode) {
- /* Try to use MFT copy. */
- u64 t64 = sbi->mft.lbo;
- sbi->mft.lbo = sbi->mft.lbo2;
- inode = ntfs_iget5(sb, &ref, NULL);
- sbi->mft.lbo = t64;
- if (IS_ERR(inode))
- inode = NULL;
- }
- if (!inode) {
- err = -EINVAL;
- ntfs_err(sb, "Failed to load $MFT.");
- goto out;
- }
- sbi->mft.ni = ntfs_i(inode);
- /* LogFile should not contains attribute list. */
- err = ni_load_all_mi(sbi->mft.ni);
- if (!err)
- err = log_replay(ni, &initialized);
- iput(inode);
- sbi->mft.ni = NULL;
- sync_blockdev(sb->s_bdev);
- invalidate_bdev(sb->s_bdev);
- if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
- err = 0;
- goto out;
- }
- if (sb_rdonly(sb) || !initialized)
- goto out;
- /* Fill LogFile by '-1' if it is initialized. */
- err = ntfs_bio_fill_1(sbi, &ni->file.run);
- out:
- sbi->flags &= ~NTFS_FLAGS_LOG_REPLAYING;
- return err;
- }
- /*
- * ntfs_look_for_free_space - Look for a free space in bitmap.
- */
- int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
- CLST *new_lcn, CLST *new_len,
- enum ALLOCATE_OPT opt)
- {
- int err;
- CLST alen;
- struct super_block *sb = sbi->sb;
- size_t alcn, zlen, zeroes, zlcn, zlen2, ztrim, new_zlen;
- struct wnd_bitmap *wnd = &sbi->used.bitmap;
- down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
- if (opt & ALLOCATE_MFT) {
- zlen = wnd_zone_len(wnd);
- if (!zlen) {
- err = ntfs_refresh_zone(sbi);
- if (err)
- goto up_write;
- zlen = wnd_zone_len(wnd);
- }
- if (!zlen) {
- ntfs_err(sbi->sb, "no free space to extend mft");
- err = -ENOSPC;
- goto up_write;
- }
- lcn = wnd_zone_bit(wnd);
- alen = min_t(CLST, len, zlen);
- wnd_zone_set(wnd, lcn + alen, zlen - alen);
- err = wnd_set_used(wnd, lcn, alen);
- if (err)
- goto up_write;
- alcn = lcn;
- goto space_found;
- }
- /*
- * 'Cause cluster 0 is always used this value means that we should use
- * cached value of 'next_free_lcn' to improve performance.
- */
- if (!lcn)
- lcn = sbi->used.next_free_lcn;
- if (lcn >= wnd->nbits)
- lcn = 0;
- alen = wnd_find(wnd, len, lcn, BITMAP_FIND_MARK_AS_USED, &alcn);
- if (alen)
- goto space_found;
- /* Try to use clusters from MftZone. */
- zlen = wnd_zone_len(wnd);
- zeroes = wnd_zeroes(wnd);
- /* Check too big request */
- if (len > zeroes + zlen || zlen <= NTFS_MIN_MFT_ZONE) {
- err = -ENOSPC;
- goto up_write;
- }
- /* How many clusters to cat from zone. */
- zlcn = wnd_zone_bit(wnd);
- zlen2 = zlen >> 1;
- ztrim = clamp_val(len, zlen2, zlen);
- new_zlen = max_t(size_t, zlen - ztrim, NTFS_MIN_MFT_ZONE);
- wnd_zone_set(wnd, zlcn, new_zlen);
- /* Allocate continues clusters. */
- alen = wnd_find(wnd, len, 0,
- BITMAP_FIND_MARK_AS_USED | BITMAP_FIND_FULL, &alcn);
- if (!alen) {
- err = -ENOSPC;
- goto up_write;
- }
- space_found:
- err = 0;
- *new_len = alen;
- *new_lcn = alcn;
- ntfs_unmap_meta(sb, alcn, alen);
- /* Set hint for next requests. */
- if (!(opt & ALLOCATE_MFT))
- sbi->used.next_free_lcn = alcn + alen;
- up_write:
- up_write(&wnd->rw_lock);
- return err;
- }
- /*
- * ntfs_check_for_free_space
- *
- * Check if it is possible to allocate 'clen' clusters and 'mlen' Mft records
- */
- bool ntfs_check_for_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen)
- {
- size_t free, zlen, avail;
- struct wnd_bitmap *wnd;
- wnd = &sbi->used.bitmap;
- down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
- free = wnd_zeroes(wnd);
- zlen = min_t(size_t, NTFS_MIN_MFT_ZONE, wnd_zone_len(wnd));
- up_read(&wnd->rw_lock);
- if (free < zlen + clen)
- return false;
- avail = free - (zlen + clen);
- wnd = &sbi->mft.bitmap;
- down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
- free = wnd_zeroes(wnd);
- zlen = wnd_zone_len(wnd);
- up_read(&wnd->rw_lock);
- if (free >= zlen + mlen)
- return true;
- return avail >= bytes_to_cluster(sbi, mlen << sbi->record_bits);
- }
- /*
- * ntfs_extend_mft - Allocate additional MFT records.
- *
- * sbi->mft.bitmap is locked for write.
- *
- * NOTE: recursive:
- * ntfs_look_free_mft ->
- * ntfs_extend_mft ->
- * attr_set_size ->
- * ni_insert_nonresident ->
- * ni_insert_attr ->
- * ni_ins_attr_ext ->
- * ntfs_look_free_mft ->
- * ntfs_extend_mft
- *
- * To avoid recursive always allocate space for two new MFT records
- * see attrib.c: "at least two MFT to avoid recursive loop".
- */
- static int ntfs_extend_mft(struct ntfs_sb_info *sbi)
- {
- int err;
- struct ntfs_inode *ni = sbi->mft.ni;
- size_t new_mft_total;
- u64 new_mft_bytes, new_bitmap_bytes;
- struct ATTRIB *attr;
- struct wnd_bitmap *wnd = &sbi->mft.bitmap;
- new_mft_total = ALIGN(wnd->nbits + NTFS_MFT_INCREASE_STEP, 128);
- new_mft_bytes = (u64)new_mft_total << sbi->record_bits;
- /* Step 1: Resize $MFT::DATA. */
- down_write(&ni->file.run_lock);
- err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
- new_mft_bytes, NULL, false, &attr);
- if (err) {
- up_write(&ni->file.run_lock);
- goto out;
- }
- attr->nres.valid_size = attr->nres.data_size;
- new_mft_total = le64_to_cpu(attr->nres.alloc_size) >> sbi->record_bits;
- ni->mi.dirty = true;
- /* Step 2: Resize $MFT::BITMAP. */
- new_bitmap_bytes = ntfs3_bitmap_size(new_mft_total);
- err = attr_set_size(ni, ATTR_BITMAP, NULL, 0, &sbi->mft.bitmap.run,
- new_bitmap_bytes, &new_bitmap_bytes, true, NULL);
- /* Refresh MFT Zone if necessary. */
- down_write_nested(&sbi->used.bitmap.rw_lock, BITMAP_MUTEX_CLUSTERS);
- ntfs_refresh_zone(sbi);
- up_write(&sbi->used.bitmap.rw_lock);
- up_write(&ni->file.run_lock);
- if (err)
- goto out;
- err = wnd_extend(wnd, new_mft_total);
- if (err)
- goto out;
- ntfs_clear_mft_tail(sbi, sbi->mft.used, new_mft_total);
- err = _ni_write_inode(&ni->vfs_inode, 0);
- out:
- return err;
- }
- /*
- * ntfs_look_free_mft - Look for a free MFT record.
- */
- int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft,
- struct ntfs_inode *ni, struct mft_inode **mi)
- {
- int err = 0;
- size_t zbit, zlen, from, to, fr;
- size_t mft_total;
- struct MFT_REF ref;
- struct super_block *sb = sbi->sb;
- struct wnd_bitmap *wnd = &sbi->mft.bitmap;
- u32 ir;
- static_assert(sizeof(sbi->mft.reserved_bitmap) * 8 >=
- MFT_REC_FREE - MFT_REC_RESERVED);
- if (!mft)
- down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
- zlen = wnd_zone_len(wnd);
- /* Always reserve space for MFT. */
- if (zlen) {
- if (mft) {
- zbit = wnd_zone_bit(wnd);
- *rno = zbit;
- wnd_zone_set(wnd, zbit + 1, zlen - 1);
- }
- goto found;
- }
- /* No MFT zone. Find the nearest to '0' free MFT. */
- if (!wnd_find(wnd, 1, MFT_REC_FREE, 0, &zbit)) {
- /* Resize MFT */
- mft_total = wnd->nbits;
- err = ntfs_extend_mft(sbi);
- if (!err) {
- zbit = mft_total;
- goto reserve_mft;
- }
- if (!mft || MFT_REC_FREE == sbi->mft.next_reserved)
- goto out;
- err = 0;
- /*
- * Look for free record reserved area [11-16) ==
- * [MFT_REC_RESERVED, MFT_REC_FREE ) MFT bitmap always
- * marks it as used.
- */
- if (!sbi->mft.reserved_bitmap) {
- /* Once per session create internal bitmap for 5 bits. */
- sbi->mft.reserved_bitmap = 0xFF;
- ref.high = 0;
- for (ir = MFT_REC_RESERVED; ir < MFT_REC_FREE; ir++) {
- struct inode *i;
- struct ntfs_inode *ni;
- struct MFT_REC *mrec;
- ref.low = cpu_to_le32(ir);
- ref.seq = cpu_to_le16(ir);
- i = ntfs_iget5(sb, &ref, NULL);
- if (IS_ERR(i)) {
- next:
- ntfs_notice(
- sb,
- "Invalid reserved record %x",
- ref.low);
- continue;
- }
- if (is_bad_inode(i)) {
- iput(i);
- goto next;
- }
- ni = ntfs_i(i);
- mrec = ni->mi.mrec;
- if (!is_rec_base(mrec))
- goto next;
- if (mrec->hard_links)
- goto next;
- if (!ni_std(ni))
- goto next;
- if (ni_find_attr(ni, NULL, NULL, ATTR_NAME,
- NULL, 0, NULL, NULL))
- goto next;
- __clear_bit(ir - MFT_REC_RESERVED,
- &sbi->mft.reserved_bitmap);
- }
- }
- /* Scan 5 bits for zero. Bit 0 == MFT_REC_RESERVED */
- zbit = find_next_zero_bit(&sbi->mft.reserved_bitmap,
- MFT_REC_FREE, MFT_REC_RESERVED);
- if (zbit >= MFT_REC_FREE) {
- sbi->mft.next_reserved = MFT_REC_FREE;
- goto out;
- }
- zlen = 1;
- sbi->mft.next_reserved = zbit;
- } else {
- reserve_mft:
- zlen = zbit == MFT_REC_FREE ? (MFT_REC_USER - MFT_REC_FREE) : 4;
- if (zbit + zlen > wnd->nbits)
- zlen = wnd->nbits - zbit;
- while (zlen > 1 && !wnd_is_free(wnd, zbit, zlen))
- zlen -= 1;
- /* [zbit, zbit + zlen) will be used for MFT itself. */
- from = sbi->mft.used;
- if (from < zbit)
- from = zbit;
- to = zbit + zlen;
- if (from < to) {
- ntfs_clear_mft_tail(sbi, from, to);
- sbi->mft.used = to;
- }
- }
- if (mft) {
- *rno = zbit;
- zbit += 1;
- zlen -= 1;
- }
- wnd_zone_set(wnd, zbit, zlen);
- found:
- if (!mft) {
- /* The request to get record for general purpose. */
- if (sbi->mft.next_free < MFT_REC_USER)
- sbi->mft.next_free = MFT_REC_USER;
- for (;;) {
- if (sbi->mft.next_free >= sbi->mft.bitmap.nbits) {
- } else if (!wnd_find(wnd, 1, MFT_REC_USER, 0, &fr)) {
- sbi->mft.next_free = sbi->mft.bitmap.nbits;
- } else {
- *rno = fr;
- sbi->mft.next_free = *rno + 1;
- break;
- }
- err = ntfs_extend_mft(sbi);
- if (err)
- goto out;
- }
- }
- if (ni && !ni_add_subrecord(ni, *rno, mi)) {
- err = -ENOMEM;
- goto out;
- }
- /* We have found a record that are not reserved for next MFT. */
- if (*rno >= MFT_REC_FREE)
- wnd_set_used(wnd, *rno, 1);
- else if (*rno >= MFT_REC_RESERVED && sbi->mft.reserved_bitmap_inited)
- __set_bit(*rno - MFT_REC_RESERVED, &sbi->mft.reserved_bitmap);
- out:
- if (!mft)
- up_write(&wnd->rw_lock);
- return err;
- }
- /*
- * ntfs_mark_rec_free - Mark record as free.
- * is_mft - true if we are changing MFT
- */
- void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft)
- {
- struct wnd_bitmap *wnd = &sbi->mft.bitmap;
- if (!is_mft)
- down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
- if (rno >= wnd->nbits)
- goto out;
- if (rno >= MFT_REC_FREE) {
- if (!wnd_is_used(wnd, rno, 1))
- ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
- else
- wnd_set_free(wnd, rno, 1);
- } else if (rno >= MFT_REC_RESERVED && sbi->mft.reserved_bitmap_inited) {
- __clear_bit(rno - MFT_REC_RESERVED, &sbi->mft.reserved_bitmap);
- }
- if (rno < wnd_zone_bit(wnd))
- wnd_zone_set(wnd, rno, 1);
- else if (rno < sbi->mft.next_free && rno >= MFT_REC_USER)
- sbi->mft.next_free = rno;
- out:
- if (!is_mft)
- up_write(&wnd->rw_lock);
- }
- /*
- * ntfs_clear_mft_tail - Format empty records [from, to).
- *
- * sbi->mft.bitmap is locked for write.
- */
- int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to)
- {
- int err;
- u32 rs;
- u64 vbo;
- struct runs_tree *run;
- struct ntfs_inode *ni;
- if (from >= to)
- return 0;
- rs = sbi->record_size;
- ni = sbi->mft.ni;
- run = &ni->file.run;
- down_read(&ni->file.run_lock);
- vbo = (u64)from * rs;
- for (; from < to; from++, vbo += rs) {
- struct ntfs_buffers nb;
- err = ntfs_get_bh(sbi, run, vbo, rs, &nb);
- if (err)
- goto out;
- err = ntfs_write_bh(sbi, &sbi->new_rec->rhdr, &nb, 0);
- nb_put(&nb);
- if (err)
- goto out;
- }
- out:
- sbi->mft.used = from;
- up_read(&ni->file.run_lock);
- return err;
- }
- /*
- * ntfs_refresh_zone - Refresh MFT zone.
- *
- * sbi->used.bitmap is locked for rw.
- * sbi->mft.bitmap is locked for write.
- * sbi->mft.ni->file.run_lock for write.
- */
- int ntfs_refresh_zone(struct ntfs_sb_info *sbi)
- {
- CLST lcn, vcn, len;
- size_t lcn_s, zlen;
- struct wnd_bitmap *wnd = &sbi->used.bitmap;
- struct ntfs_inode *ni = sbi->mft.ni;
- /* Do not change anything unless we have non empty MFT zone. */
- if (wnd_zone_len(wnd))
- return 0;
- vcn = bytes_to_cluster(sbi,
- (u64)sbi->mft.bitmap.nbits << sbi->record_bits);
- if (!run_lookup_entry(&ni->file.run, vcn - 1, &lcn, &len, NULL))
- lcn = SPARSE_LCN;
- /* We should always find Last Lcn for MFT. */
- if (lcn == SPARSE_LCN)
- return -EINVAL;
- lcn_s = lcn + 1;
- /* Try to allocate clusters after last MFT run. */
- zlen = wnd_find(wnd, sbi->zone_max, lcn_s, 0, &lcn_s);
- wnd_zone_set(wnd, lcn_s, zlen);
- return 0;
- }
- /*
- * ntfs_update_mftmirr - Update $MFTMirr data.
- */
- void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait)
- {
- int err;
- struct super_block *sb = sbi->sb;
- u32 blocksize, bytes;
- sector_t block1, block2;
- /*
- * sb can be NULL here. In this case sbi->flags should be 0 too.
- */
- if (!sb || !(sbi->flags & NTFS_FLAGS_MFTMIRR) ||
- unlikely(ntfs3_forced_shutdown(sb)))
- return;
- blocksize = sb->s_blocksize;
- bytes = sbi->mft.recs_mirr << sbi->record_bits;
- block1 = sbi->mft.lbo >> sb->s_blocksize_bits;
- block2 = sbi->mft.lbo2 >> sb->s_blocksize_bits;
- for (; bytes >= blocksize; bytes -= blocksize) {
- struct buffer_head *bh1, *bh2;
- bh1 = sb_bread(sb, block1++);
- if (!bh1)
- return;
- bh2 = sb_getblk(sb, block2++);
- if (!bh2) {
- put_bh(bh1);
- return;
- }
- if (buffer_locked(bh2))
- __wait_on_buffer(bh2);
- lock_buffer(bh2);
- memcpy(bh2->b_data, bh1->b_data, blocksize);
- set_buffer_uptodate(bh2);
- mark_buffer_dirty(bh2);
- unlock_buffer(bh2);
- put_bh(bh1);
- bh1 = NULL;
- err = wait ? sync_dirty_buffer(bh2) : 0;
- put_bh(bh2);
- if (err)
- return;
- }
- sbi->flags &= ~NTFS_FLAGS_MFTMIRR;
- }
- /*
- * ntfs_bad_inode
- *
- * Marks inode as bad and marks fs as 'dirty'
- */
- void ntfs_bad_inode(struct inode *inode, const char *hint)
- {
- struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
- ntfs_inode_err(inode, "%s", hint);
- make_bad_inode(inode);
- /* Avoid recursion if bad inode is $Volume. */
- if (inode->i_ino != MFT_REC_VOL &&
- !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING)) {
- ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
- }
- }
- /*
- * ntfs_set_state
- *
- * Mount: ntfs_set_state(NTFS_DIRTY_DIRTY)
- * Umount: ntfs_set_state(NTFS_DIRTY_CLEAR)
- * NTFS error: ntfs_set_state(NTFS_DIRTY_ERROR)
- */
- int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty)
- {
- int err;
- struct ATTRIB *attr;
- struct VOLUME_INFO *info;
- struct mft_inode *mi;
- struct ntfs_inode *ni;
- __le16 info_flags;
- /*
- * Do not change state if fs was real_dirty.
- * Do not change state if fs already dirty(clear).
- * Do not change any thing if mounted read only.
- */
- if (sbi->volume.real_dirty || sb_rdonly(sbi->sb))
- return 0;
- /* Check cached value. */
- if ((dirty == NTFS_DIRTY_CLEAR ? 0 : VOLUME_FLAG_DIRTY) ==
- (sbi->volume.flags & VOLUME_FLAG_DIRTY))
- return 0;
- ni = sbi->volume.ni;
- if (!ni)
- return -EINVAL;
- mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_DIRTY);
- attr = ni_find_attr(ni, NULL, NULL, ATTR_VOL_INFO, NULL, 0, NULL, &mi);
- if (!attr) {
- err = -EINVAL;
- goto out;
- }
- info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
- if (!info) {
- err = -EINVAL;
- goto out;
- }
- info_flags = info->flags;
- switch (dirty) {
- case NTFS_DIRTY_ERROR:
- ntfs_notice(sbi->sb, "Mark volume as dirty due to NTFS errors");
- sbi->volume.real_dirty = true;
- fallthrough;
- case NTFS_DIRTY_DIRTY:
- info->flags |= VOLUME_FLAG_DIRTY;
- break;
- case NTFS_DIRTY_CLEAR:
- info->flags &= ~VOLUME_FLAG_DIRTY;
- break;
- }
- /* Cache current volume flags. */
- if (info_flags != info->flags) {
- sbi->volume.flags = info->flags;
- mi->dirty = true;
- }
- err = 0;
- out:
- ni_unlock(ni);
- if (err)
- return err;
- mark_inode_dirty_sync(&ni->vfs_inode);
- /* verify(!ntfs_update_mftmirr()); */
- /* write mft record on disk. */
- err = _ni_write_inode(&ni->vfs_inode, 1);
- return err;
- }
- /*
- * security_hash - Calculates a hash of security descriptor.
- */
- static inline __le32 security_hash(const void *sd, size_t bytes)
- {
- u32 hash = 0;
- const __le32 *ptr = sd;
- bytes >>= 2;
- while (bytes--)
- hash = ((hash >> 0x1D) | (hash << 3)) + le32_to_cpu(*ptr++);
- return cpu_to_le32(hash);
- }
- /*
- * simple wrapper for sb_bread_unmovable.
- */
- struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block)
- {
- struct ntfs_sb_info *sbi = sb->s_fs_info;
- struct buffer_head *bh;
- if (unlikely(block >= sbi->volume.blocks)) {
- /* prevent generic message "attempt to access beyond end of device" */
- ntfs_err(sb, "try to read out of volume at offset 0x%llx",
- (u64)block << sb->s_blocksize_bits);
- return NULL;
- }
- bh = sb_bread_unmovable(sb, block);
- if (bh)
- return bh;
- ntfs_err(sb, "failed to read volume at offset 0x%llx",
- (u64)block << sb->s_blocksize_bits);
- return NULL;
- }
- int ntfs_sb_read(struct super_block *sb, u64 lbo, size_t bytes, void *buffer)
- {
- struct block_device *bdev = sb->s_bdev;
- u32 blocksize = sb->s_blocksize;
- u64 block = lbo >> sb->s_blocksize_bits;
- u32 off = lbo & (blocksize - 1);
- u32 op = blocksize - off;
- for (; bytes; block += 1, off = 0, op = blocksize) {
- struct buffer_head *bh = __bread(bdev, block, blocksize);
- if (!bh)
- return -EIO;
- if (op > bytes)
- op = bytes;
- memcpy(buffer, bh->b_data + off, op);
- put_bh(bh);
- bytes -= op;
- buffer = Add2Ptr(buffer, op);
- }
- return 0;
- }
- int ntfs_sb_write(struct super_block *sb, u64 lbo, size_t bytes,
- const void *buf, int wait)
- {
- u32 blocksize = sb->s_blocksize;
- struct block_device *bdev = sb->s_bdev;
- sector_t block = lbo >> sb->s_blocksize_bits;
- u32 off = lbo & (blocksize - 1);
- u32 op = blocksize - off;
- struct buffer_head *bh;
- if (!wait && (sb->s_flags & SB_SYNCHRONOUS))
- wait = 1;
- for (; bytes; block += 1, off = 0, op = blocksize) {
- if (op > bytes)
- op = bytes;
- if (op < blocksize) {
- bh = __bread(bdev, block, blocksize);
- if (!bh) {
- ntfs_err(sb, "failed to read block %llx",
- (u64)block);
- return -EIO;
- }
- } else {
- bh = __getblk(bdev, block, blocksize);
- if (!bh)
- return -ENOMEM;
- }
- if (buffer_locked(bh))
- __wait_on_buffer(bh);
- lock_buffer(bh);
- if (buf) {
- memcpy(bh->b_data + off, buf, op);
- buf = Add2Ptr(buf, op);
- } else {
- memset(bh->b_data + off, -1, op);
- }
- set_buffer_uptodate(bh);
- mark_buffer_dirty(bh);
- unlock_buffer(bh);
- if (wait) {
- int err = sync_dirty_buffer(bh);
- if (err) {
- ntfs_err(
- sb,
- "failed to sync buffer at block %llx, error %d",
- (u64)block, err);
- put_bh(bh);
- return err;
- }
- }
- put_bh(bh);
- bytes -= op;
- }
- return 0;
- }
- int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
- u64 vbo, const void *buf, size_t bytes, int sync)
- {
- struct super_block *sb = sbi->sb;
- u8 cluster_bits = sbi->cluster_bits;
- u32 off = vbo & sbi->cluster_mask;
- CLST lcn, clen, vcn = vbo >> cluster_bits, vcn_next;
- u64 lbo, len;
- size_t idx;
- if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
- return -ENOENT;
- if (lcn == SPARSE_LCN)
- return -EINVAL;
- lbo = ((u64)lcn << cluster_bits) + off;
- len = ((u64)clen << cluster_bits) - off;
- for (;;) {
- u32 op = min_t(u64, len, bytes);
- int err = ntfs_sb_write(sb, lbo, op, buf, sync);
- if (err)
- return err;
- bytes -= op;
- if (!bytes)
- break;
- vcn_next = vcn + clen;
- if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
- vcn != vcn_next)
- return -ENOENT;
- if (lcn == SPARSE_LCN)
- return -EINVAL;
- if (buf)
- buf = Add2Ptr(buf, op);
- lbo = ((u64)lcn << cluster_bits);
- len = ((u64)clen << cluster_bits);
- }
- return 0;
- }
- struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
- const struct runs_tree *run, u64 vbo)
- {
- struct super_block *sb = sbi->sb;
- u8 cluster_bits = sbi->cluster_bits;
- CLST lcn;
- u64 lbo;
- if (!run_lookup_entry(run, vbo >> cluster_bits, &lcn, NULL, NULL))
- return ERR_PTR(-ENOENT);
- lbo = ((u64)lcn << cluster_bits) + (vbo & sbi->cluster_mask);
- return ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
- }
- int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
- u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb)
- {
- int err;
- struct super_block *sb = sbi->sb;
- u32 blocksize = sb->s_blocksize;
- u8 cluster_bits = sbi->cluster_bits;
- u32 off = vbo & sbi->cluster_mask;
- u32 nbh = 0;
- CLST vcn_next, vcn = vbo >> cluster_bits;
- CLST lcn, clen;
- u64 lbo, len;
- size_t idx;
- struct buffer_head *bh;
- if (!run) {
- /* First reading of $Volume + $MFTMirr + $LogFile goes here. */
- if (vbo > MFT_REC_VOL * sbi->record_size) {
- err = -ENOENT;
- goto out;
- }
- /* Use absolute boot's 'MFTCluster' to read record. */
- lbo = vbo + sbi->mft.lbo;
- len = sbi->record_size;
- } else if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
- err = -ENOENT;
- goto out;
- } else {
- if (lcn == SPARSE_LCN) {
- err = -EINVAL;
- goto out;
- }
- lbo = ((u64)lcn << cluster_bits) + off;
- len = ((u64)clen << cluster_bits) - off;
- }
- off = lbo & (blocksize - 1);
- if (nb) {
- nb->off = off;
- nb->bytes = bytes;
- }
- for (;;) {
- u32 len32 = len >= bytes ? bytes : len;
- sector_t block = lbo >> sb->s_blocksize_bits;
- do {
- u32 op = blocksize - off;
- if (op > len32)
- op = len32;
- bh = ntfs_bread(sb, block);
- if (!bh) {
- err = -EIO;
- goto out;
- }
- if (buf) {
- memcpy(buf, bh->b_data + off, op);
- buf = Add2Ptr(buf, op);
- }
- if (!nb) {
- put_bh(bh);
- } else if (nbh >= ARRAY_SIZE(nb->bh)) {
- err = -EINVAL;
- goto out;
- } else {
- nb->bh[nbh++] = bh;
- nb->nbufs = nbh;
- }
- bytes -= op;
- if (!bytes)
- return 0;
- len32 -= op;
- block += 1;
- off = 0;
- } while (len32);
- vcn_next = vcn + clen;
- if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
- vcn != vcn_next) {
- err = -ENOENT;
- goto out;
- }
- if (lcn == SPARSE_LCN) {
- err = -EINVAL;
- goto out;
- }
- lbo = ((u64)lcn << cluster_bits);
- len = ((u64)clen << cluster_bits);
- }
- out:
- if (!nbh)
- return err;
- while (nbh) {
- put_bh(nb->bh[--nbh]);
- nb->bh[nbh] = NULL;
- }
- nb->nbufs = 0;
- return err;
- }
- /*
- * ntfs_read_bh
- *
- * Return: < 0 if error, 0 if ok, -E_NTFS_FIXUP if need to update fixups.
- */
- int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
- struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
- struct ntfs_buffers *nb)
- {
- int err = ntfs_read_run_nb(sbi, run, vbo, rhdr, bytes, nb);
- if (err)
- return err;
- return ntfs_fix_post_read(rhdr, nb->bytes, true);
- }
- int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
- u32 bytes, struct ntfs_buffers *nb)
- {
- int err = 0;
- struct super_block *sb = sbi->sb;
- u32 blocksize = sb->s_blocksize;
- u8 cluster_bits = sbi->cluster_bits;
- CLST vcn_next, vcn = vbo >> cluster_bits;
- u32 off;
- u32 nbh = 0;
- CLST lcn, clen;
- u64 lbo, len;
- size_t idx;
- nb->bytes = bytes;
- if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
- err = -ENOENT;
- goto out;
- }
- off = vbo & sbi->cluster_mask;
- lbo = ((u64)lcn << cluster_bits) + off;
- len = ((u64)clen << cluster_bits) - off;
- nb->off = off = lbo & (blocksize - 1);
- for (;;) {
- u32 len32 = min_t(u64, len, bytes);
- sector_t block = lbo >> sb->s_blocksize_bits;
- do {
- u32 op;
- struct buffer_head *bh;
- if (nbh >= ARRAY_SIZE(nb->bh)) {
- err = -EINVAL;
- goto out;
- }
- op = blocksize - off;
- if (op > len32)
- op = len32;
- if (op == blocksize) {
- bh = sb_getblk(sb, block);
- if (!bh) {
- err = -ENOMEM;
- goto out;
- }
- if (buffer_locked(bh))
- __wait_on_buffer(bh);
- set_buffer_uptodate(bh);
- } else {
- bh = ntfs_bread(sb, block);
- if (!bh) {
- err = -EIO;
- goto out;
- }
- }
- nb->bh[nbh++] = bh;
- bytes -= op;
- if (!bytes) {
- nb->nbufs = nbh;
- return 0;
- }
- block += 1;
- len32 -= op;
- off = 0;
- } while (len32);
- vcn_next = vcn + clen;
- if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
- vcn != vcn_next) {
- err = -ENOENT;
- goto out;
- }
- lbo = ((u64)lcn << cluster_bits);
- len = ((u64)clen << cluster_bits);
- }
- out:
- while (nbh) {
- put_bh(nb->bh[--nbh]);
- nb->bh[nbh] = NULL;
- }
- nb->nbufs = 0;
- return err;
- }
- int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
- struct ntfs_buffers *nb, int sync)
- {
- int err = 0;
- struct super_block *sb = sbi->sb;
- u32 block_size = sb->s_blocksize;
- u32 bytes = nb->bytes;
- u32 off = nb->off;
- u16 fo = le16_to_cpu(rhdr->fix_off);
- u16 fn = le16_to_cpu(rhdr->fix_num);
- u32 idx;
- __le16 *fixup;
- __le16 sample;
- if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
- fn * SECTOR_SIZE > bytes) {
- return -EINVAL;
- }
- for (idx = 0; bytes && idx < nb->nbufs; idx += 1, off = 0) {
- u32 op = block_size - off;
- char *bh_data;
- struct buffer_head *bh = nb->bh[idx];
- __le16 *ptr, *end_data;
- if (op > bytes)
- op = bytes;
- if (buffer_locked(bh))
- __wait_on_buffer(bh);
- lock_buffer(bh);
- bh_data = bh->b_data + off;
- end_data = Add2Ptr(bh_data, op);
- memcpy(bh_data, rhdr, op);
- if (!idx) {
- u16 t16;
- fixup = Add2Ptr(bh_data, fo);
- sample = *fixup;
- t16 = le16_to_cpu(sample);
- if (t16 >= 0x7FFF) {
- sample = *fixup = cpu_to_le16(1);
- } else {
- sample = cpu_to_le16(t16 + 1);
- *fixup = sample;
- }
- *(__le16 *)Add2Ptr(rhdr, fo) = sample;
- }
- ptr = Add2Ptr(bh_data, SECTOR_SIZE - sizeof(short));
- do {
- *++fixup = *ptr;
- *ptr = sample;
- ptr += SECTOR_SIZE / sizeof(short);
- } while (ptr < end_data);
- set_buffer_uptodate(bh);
- mark_buffer_dirty(bh);
- unlock_buffer(bh);
- if (sync) {
- int err2 = sync_dirty_buffer(bh);
- if (!err && err2)
- err = err2;
- }
- bytes -= op;
- rhdr = Add2Ptr(rhdr, op);
- }
- return err;
- }
- /*
- * ntfs_bio_pages - Read/write pages from/to disk.
- */
- int ntfs_bio_pages(struct ntfs_sb_info *sbi, const struct runs_tree *run,
- struct page **pages, u32 nr_pages, u64 vbo, u32 bytes,
- enum req_op op)
- {
- int err = 0;
- struct bio *new, *bio = NULL;
- struct super_block *sb = sbi->sb;
- struct block_device *bdev = sb->s_bdev;
- struct page *page;
- u8 cluster_bits = sbi->cluster_bits;
- CLST lcn, clen, vcn, vcn_next;
- u32 add, off, page_idx;
- u64 lbo, len;
- size_t run_idx;
- struct blk_plug plug;
- if (!bytes)
- return 0;
- blk_start_plug(&plug);
- /* Align vbo and bytes to be 512 bytes aligned. */
- lbo = (vbo + bytes + 511) & ~511ull;
- vbo = vbo & ~511ull;
- bytes = lbo - vbo;
- vcn = vbo >> cluster_bits;
- if (!run_lookup_entry(run, vcn, &lcn, &clen, &run_idx)) {
- err = -ENOENT;
- goto out;
- }
- off = vbo & sbi->cluster_mask;
- page_idx = 0;
- page = pages[0];
- for (;;) {
- lbo = ((u64)lcn << cluster_bits) + off;
- len = ((u64)clen << cluster_bits) - off;
- new_bio:
- new = bio_alloc(bdev, nr_pages - page_idx, op, GFP_NOFS);
- if (bio) {
- bio_chain(bio, new);
- submit_bio(bio);
- }
- bio = new;
- bio->bi_iter.bi_sector = lbo >> 9;
- while (len) {
- off = vbo & (PAGE_SIZE - 1);
- add = off + len > PAGE_SIZE ? (PAGE_SIZE - off) : len;
- if (bio_add_page(bio, page, add, off) < add)
- goto new_bio;
- if (bytes <= add)
- goto out;
- bytes -= add;
- vbo += add;
- if (add + off == PAGE_SIZE) {
- page_idx += 1;
- if (WARN_ON(page_idx >= nr_pages)) {
- err = -EINVAL;
- goto out;
- }
- page = pages[page_idx];
- }
- if (len <= add)
- break;
- len -= add;
- lbo += add;
- }
- vcn_next = vcn + clen;
- if (!run_get_entry(run, ++run_idx, &vcn, &lcn, &clen) ||
- vcn != vcn_next) {
- err = -ENOENT;
- goto out;
- }
- off = 0;
- }
- out:
- if (bio) {
- if (!err)
- err = submit_bio_wait(bio);
- bio_put(bio);
- }
- blk_finish_plug(&plug);
- return err;
- }
- /*
- * ntfs_bio_fill_1 - Helper for ntfs_loadlog_and_replay().
- *
- * Fill on-disk logfile range by (-1)
- * this means empty logfile.
- */
- int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const struct runs_tree *run)
- {
- int err = 0;
- struct super_block *sb = sbi->sb;
- struct block_device *bdev = sb->s_bdev;
- u8 cluster_bits = sbi->cluster_bits;
- struct bio *new, *bio = NULL;
- CLST lcn, clen;
- u64 lbo, len;
- size_t run_idx;
- struct page *fill;
- void *kaddr;
- struct blk_plug plug;
- fill = alloc_page(GFP_KERNEL);
- if (!fill)
- return -ENOMEM;
- kaddr = kmap_atomic(fill);
- memset(kaddr, -1, PAGE_SIZE);
- kunmap_atomic(kaddr);
- flush_dcache_page(fill);
- lock_page(fill);
- if (!run_lookup_entry(run, 0, &lcn, &clen, &run_idx)) {
- err = -ENOENT;
- goto out;
- }
- /*
- * TODO: Try blkdev_issue_write_same.
- */
- blk_start_plug(&plug);
- do {
- lbo = (u64)lcn << cluster_bits;
- len = (u64)clen << cluster_bits;
- new_bio:
- new = bio_alloc(bdev, BIO_MAX_VECS, REQ_OP_WRITE, GFP_NOFS);
- if (bio) {
- bio_chain(bio, new);
- submit_bio(bio);
- }
- bio = new;
- bio->bi_iter.bi_sector = lbo >> 9;
- for (;;) {
- u32 add = len > PAGE_SIZE ? PAGE_SIZE : len;
- if (bio_add_page(bio, fill, add, 0) < add)
- goto new_bio;
- lbo += add;
- if (len <= add)
- break;
- len -= add;
- }
- } while (run_get_entry(run, ++run_idx, NULL, &lcn, &clen));
- if (!err)
- err = submit_bio_wait(bio);
- bio_put(bio);
- blk_finish_plug(&plug);
- out:
- unlock_page(fill);
- put_page(fill);
- return err;
- }
- int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
- u64 vbo, u64 *lbo, u64 *bytes)
- {
- u32 off;
- CLST lcn, len;
- u8 cluster_bits = sbi->cluster_bits;
- if (!run_lookup_entry(run, vbo >> cluster_bits, &lcn, &len, NULL))
- return -ENOENT;
- off = vbo & sbi->cluster_mask;
- *lbo = lcn == SPARSE_LCN ? -1 : (((u64)lcn << cluster_bits) + off);
- *bytes = ((u64)len << cluster_bits) - off;
- return 0;
- }
- struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno,
- enum RECORD_FLAG flag)
- {
- int err = 0;
- struct super_block *sb = sbi->sb;
- struct inode *inode = new_inode(sb);
- struct ntfs_inode *ni;
- if (!inode)
- return ERR_PTR(-ENOMEM);
- ni = ntfs_i(inode);
- err = mi_format_new(&ni->mi, sbi, rno, flag, false);
- if (err)
- goto out;
- inode->i_ino = rno;
- if (insert_inode_locked(inode) < 0) {
- err = -EIO;
- goto out;
- }
- out:
- if (err) {
- make_bad_inode(inode);
- iput(inode);
- ni = ERR_PTR(err);
- }
- return ni;
- }
- /*
- * O:BAG:BAD:(A;OICI;FA;;;WD)
- * Owner S-1-5-32-544 (Administrators)
- * Group S-1-5-32-544 (Administrators)
- * ACE: allow S-1-1-0 (Everyone) with FILE_ALL_ACCESS
- */
- const u8 s_default_security[] __aligned(8) = {
- 0x01, 0x00, 0x04, 0x80, 0x30, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x02, 0x00, 0x1C, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x14, 0x00, 0xFF, 0x01, 0x1F, 0x00,
- 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00,
- 0x20, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
- 0x20, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00,
- };
- static_assert(sizeof(s_default_security) == 0x50);
- static inline u32 sid_length(const struct SID *sid)
- {
- return struct_size(sid, SubAuthority, sid->SubAuthorityCount);
- }
- /*
- * is_acl_valid
- *
- * Thanks Mark Harmstone for idea.
- */
- static bool is_acl_valid(const struct ACL *acl, u32 len)
- {
- const struct ACE_HEADER *ace;
- u32 i;
- u16 ace_count, ace_size;
- if (acl->AclRevision != ACL_REVISION &&
- acl->AclRevision != ACL_REVISION_DS) {
- /*
- * This value should be ACL_REVISION, unless the ACL contains an
- * object-specific ACE, in which case this value must be ACL_REVISION_DS.
- * All ACEs in an ACL must be at the same revision level.
- */
- return false;
- }
- if (acl->Sbz1)
- return false;
- if (le16_to_cpu(acl->AclSize) > len)
- return false;
- if (acl->Sbz2)
- return false;
- len -= sizeof(struct ACL);
- ace = (struct ACE_HEADER *)&acl[1];
- ace_count = le16_to_cpu(acl->AceCount);
- for (i = 0; i < ace_count; i++) {
- if (len < sizeof(struct ACE_HEADER))
- return false;
- ace_size = le16_to_cpu(ace->AceSize);
- if (len < ace_size)
- return false;
- len -= ace_size;
- ace = Add2Ptr(ace, ace_size);
- }
- return true;
- }
- bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len)
- {
- u32 sd_owner, sd_group, sd_sacl, sd_dacl;
- if (len < sizeof(struct SECURITY_DESCRIPTOR_RELATIVE))
- return false;
- if (sd->Revision != 1)
- return false;
- if (sd->Sbz1)
- return false;
- if (!(sd->Control & SE_SELF_RELATIVE))
- return false;
- sd_owner = le32_to_cpu(sd->Owner);
- if (sd_owner) {
- const struct SID *owner = Add2Ptr(sd, sd_owner);
- if (sd_owner + offsetof(struct SID, SubAuthority) > len)
- return false;
- if (owner->Revision != 1)
- return false;
- if (sd_owner + sid_length(owner) > len)
- return false;
- }
- sd_group = le32_to_cpu(sd->Group);
- if (sd_group) {
- const struct SID *group = Add2Ptr(sd, sd_group);
- if (sd_group + offsetof(struct SID, SubAuthority) > len)
- return false;
- if (group->Revision != 1)
- return false;
- if (sd_group + sid_length(group) > len)
- return false;
- }
- sd_sacl = le32_to_cpu(sd->Sacl);
- if (sd_sacl) {
- const struct ACL *sacl = Add2Ptr(sd, sd_sacl);
- if (sd_sacl + sizeof(struct ACL) > len)
- return false;
- if (!is_acl_valid(sacl, len - sd_sacl))
- return false;
- }
- sd_dacl = le32_to_cpu(sd->Dacl);
- if (sd_dacl) {
- const struct ACL *dacl = Add2Ptr(sd, sd_dacl);
- if (sd_dacl + sizeof(struct ACL) > len)
- return false;
- if (!is_acl_valid(dacl, len - sd_dacl))
- return false;
- }
- return true;
- }
- /*
- * ntfs_security_init - Load and parse $Secure.
- */
- int ntfs_security_init(struct ntfs_sb_info *sbi)
- {
- int err;
- struct super_block *sb = sbi->sb;
- struct inode *inode;
- struct ntfs_inode *ni;
- struct MFT_REF ref;
- struct ATTRIB *attr;
- struct ATTR_LIST_ENTRY *le;
- u64 sds_size;
- size_t off;
- struct NTFS_DE *ne;
- struct NTFS_DE_SII *sii_e;
- struct ntfs_fnd *fnd_sii = NULL;
- const struct INDEX_ROOT *root_sii;
- const struct INDEX_ROOT *root_sdh;
- struct ntfs_index *indx_sdh = &sbi->security.index_sdh;
- struct ntfs_index *indx_sii = &sbi->security.index_sii;
- ref.low = cpu_to_le32(MFT_REC_SECURE);
- ref.high = 0;
- ref.seq = cpu_to_le16(MFT_REC_SECURE);
- inode = ntfs_iget5(sb, &ref, &NAME_SECURE);
- if (IS_ERR(inode)) {
- err = PTR_ERR(inode);
- ntfs_err(sb, "Failed to load $Secure (%d).", err);
- inode = NULL;
- goto out;
- }
- ni = ntfs_i(inode);
- le = NULL;
- attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SDH_NAME,
- ARRAY_SIZE(SDH_NAME), NULL, NULL);
- if (!attr ||
- !(root_sdh = resident_data_ex(attr, sizeof(struct INDEX_ROOT))) ||
- root_sdh->type != ATTR_ZERO ||
- root_sdh->rule != NTFS_COLLATION_TYPE_SECURITY_HASH ||
- offsetof(struct INDEX_ROOT, ihdr) +
- le32_to_cpu(root_sdh->ihdr.used) >
- le32_to_cpu(attr->res.data_size)) {
- ntfs_err(sb, "$Secure::$SDH is corrupted.");
- err = -EINVAL;
- goto out;
- }
- err = indx_init(indx_sdh, sbi, attr, INDEX_MUTEX_SDH);
- if (err) {
- ntfs_err(sb, "Failed to initialize $Secure::$SDH (%d).", err);
- goto out;
- }
- attr = ni_find_attr(ni, attr, &le, ATTR_ROOT, SII_NAME,
- ARRAY_SIZE(SII_NAME), NULL, NULL);
- if (!attr ||
- !(root_sii = resident_data_ex(attr, sizeof(struct INDEX_ROOT))) ||
- root_sii->type != ATTR_ZERO ||
- root_sii->rule != NTFS_COLLATION_TYPE_UINT ||
- offsetof(struct INDEX_ROOT, ihdr) +
- le32_to_cpu(root_sii->ihdr.used) >
- le32_to_cpu(attr->res.data_size)) {
- ntfs_err(sb, "$Secure::$SII is corrupted.");
- err = -EINVAL;
- goto out;
- }
- err = indx_init(indx_sii, sbi, attr, INDEX_MUTEX_SII);
- if (err) {
- ntfs_err(sb, "Failed to initialize $Secure::$SII (%d).", err);
- goto out;
- }
- fnd_sii = fnd_get();
- if (!fnd_sii) {
- err = -ENOMEM;
- goto out;
- }
- sds_size = inode->i_size;
- /* Find the last valid Id. */
- sbi->security.next_id = SECURITY_ID_FIRST;
- /* Always write new security at the end of bucket. */
- sbi->security.next_off =
- ALIGN(sds_size - SecurityDescriptorsBlockSize, 16);
- off = 0;
- ne = NULL;
- for (;;) {
- u32 next_id;
- err = indx_find_raw(indx_sii, ni, root_sii, &ne, &off, fnd_sii);
- if (err || !ne)
- break;
- sii_e = (struct NTFS_DE_SII *)ne;
- if (le16_to_cpu(ne->view.data_size) < sizeof(sii_e->sec_hdr))
- continue;
- next_id = le32_to_cpu(sii_e->sec_id) + 1;
- if (next_id >= sbi->security.next_id)
- sbi->security.next_id = next_id;
- }
- sbi->security.ni = ni;
- inode = NULL;
- out:
- iput(inode);
- fnd_put(fnd_sii);
- return err;
- }
- /*
- * ntfs_get_security_by_id - Read security descriptor by id.
- */
- int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
- struct SECURITY_DESCRIPTOR_RELATIVE **sd,
- size_t *size)
- {
- int err;
- int diff;
- struct ntfs_inode *ni = sbi->security.ni;
- struct ntfs_index *indx = &sbi->security.index_sii;
- void *p = NULL;
- struct NTFS_DE_SII *sii_e;
- struct ntfs_fnd *fnd_sii;
- struct SECURITY_HDR d_security;
- const struct INDEX_ROOT *root_sii;
- u32 t32;
- *sd = NULL;
- mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_SECURITY);
- fnd_sii = fnd_get();
- if (!fnd_sii) {
- err = -ENOMEM;
- goto out;
- }
- root_sii = indx_get_root(indx, ni, NULL, NULL);
- if (!root_sii) {
- err = -EINVAL;
- goto out;
- }
- /* Try to find this SECURITY descriptor in SII indexes. */
- err = indx_find(indx, ni, root_sii, &security_id, sizeof(security_id),
- NULL, &diff, (struct NTFS_DE **)&sii_e, fnd_sii);
- if (err)
- goto out;
- if (diff)
- goto out;
- t32 = le32_to_cpu(sii_e->sec_hdr.size);
- if (t32 < sizeof(struct SECURITY_HDR)) {
- err = -EINVAL;
- goto out;
- }
- if (t32 > sizeof(struct SECURITY_HDR) + 0x10000) {
- /* Looks like too big security. 0x10000 - is arbitrary big number. */
- err = -EFBIG;
- goto out;
- }
- *size = t32 - sizeof(struct SECURITY_HDR);
- p = kmalloc(*size, GFP_NOFS);
- if (!p) {
- err = -ENOMEM;
- goto out;
- }
- err = ntfs_read_run_nb(sbi, &ni->file.run,
- le64_to_cpu(sii_e->sec_hdr.off), &d_security,
- sizeof(d_security), NULL);
- if (err)
- goto out;
- if (memcmp(&d_security, &sii_e->sec_hdr, sizeof(d_security))) {
- err = -EINVAL;
- goto out;
- }
- err = ntfs_read_run_nb(sbi, &ni->file.run,
- le64_to_cpu(sii_e->sec_hdr.off) +
- sizeof(struct SECURITY_HDR),
- p, *size, NULL);
- if (err)
- goto out;
- *sd = p;
- p = NULL;
- out:
- kfree(p);
- fnd_put(fnd_sii);
- ni_unlock(ni);
- return err;
- }
- /*
- * ntfs_insert_security - Insert security descriptor into $Secure::SDS.
- *
- * SECURITY Descriptor Stream data is organized into chunks of 256K bytes
- * and it contains a mirror copy of each security descriptor. When writing
- * to a security descriptor at location X, another copy will be written at
- * location (X+256K).
- * When writing a security descriptor that will cross the 256K boundary,
- * the pointer will be advanced by 256K to skip
- * over the mirror portion.
- */
- int ntfs_insert_security(struct ntfs_sb_info *sbi,
- const struct SECURITY_DESCRIPTOR_RELATIVE *sd,
- u32 size_sd, __le32 *security_id, bool *inserted)
- {
- int err, diff;
- struct ntfs_inode *ni = sbi->security.ni;
- struct ntfs_index *indx_sdh = &sbi->security.index_sdh;
- struct ntfs_index *indx_sii = &sbi->security.index_sii;
- struct NTFS_DE_SDH *e;
- struct NTFS_DE_SDH sdh_e;
- struct NTFS_DE_SII sii_e;
- struct SECURITY_HDR *d_security;
- u32 new_sec_size = size_sd + sizeof(struct SECURITY_HDR);
- u32 aligned_sec_size = ALIGN(new_sec_size, 16);
- struct SECURITY_KEY hash_key;
- struct ntfs_fnd *fnd_sdh = NULL;
- const struct INDEX_ROOT *root_sdh;
- const struct INDEX_ROOT *root_sii;
- u64 mirr_off, new_sds_size;
- u32 next, left;
- static_assert((1 << Log2OfSecurityDescriptorsBlockSize) ==
- SecurityDescriptorsBlockSize);
- hash_key.hash = security_hash(sd, size_sd);
- hash_key.sec_id = SECURITY_ID_INVALID;
- if (inserted)
- *inserted = false;
- *security_id = SECURITY_ID_INVALID;
- /* Allocate a temporal buffer. */
- d_security = kzalloc(aligned_sec_size, GFP_NOFS);
- if (!d_security)
- return -ENOMEM;
- mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_SECURITY);
- fnd_sdh = fnd_get();
- if (!fnd_sdh) {
- err = -ENOMEM;
- goto out;
- }
- root_sdh = indx_get_root(indx_sdh, ni, NULL, NULL);
- if (!root_sdh) {
- err = -EINVAL;
- goto out;
- }
- root_sii = indx_get_root(indx_sii, ni, NULL, NULL);
- if (!root_sii) {
- err = -EINVAL;
- goto out;
- }
- /*
- * Check if such security already exists.
- * Use "SDH" and hash -> to get the offset in "SDS".
- */
- err = indx_find(indx_sdh, ni, root_sdh, &hash_key, sizeof(hash_key),
- &d_security->key.sec_id, &diff, (struct NTFS_DE **)&e,
- fnd_sdh);
- if (err)
- goto out;
- while (e) {
- if (le32_to_cpu(e->sec_hdr.size) == new_sec_size) {
- err = ntfs_read_run_nb(sbi, &ni->file.run,
- le64_to_cpu(e->sec_hdr.off),
- d_security, new_sec_size, NULL);
- if (err)
- goto out;
- if (le32_to_cpu(d_security->size) == new_sec_size &&
- d_security->key.hash == hash_key.hash &&
- !memcmp(d_security + 1, sd, size_sd)) {
- /* Such security already exists. */
- *security_id = d_security->key.sec_id;
- err = 0;
- goto out;
- }
- }
- err = indx_find_sort(indx_sdh, ni, root_sdh,
- (struct NTFS_DE **)&e, fnd_sdh);
- if (err)
- goto out;
- if (!e || e->key.hash != hash_key.hash)
- break;
- }
- /* Zero unused space. */
- next = sbi->security.next_off & (SecurityDescriptorsBlockSize - 1);
- left = SecurityDescriptorsBlockSize - next;
- /* Zero gap until SecurityDescriptorsBlockSize. */
- if (left < new_sec_size) {
- /* Zero "left" bytes from sbi->security.next_off. */
- sbi->security.next_off += SecurityDescriptorsBlockSize + left;
- }
- /* Zero tail of previous security. */
- //used = ni->vfs_inode.i_size & (SecurityDescriptorsBlockSize - 1);
- /*
- * Example:
- * 0x40438 == ni->vfs_inode.i_size
- * 0x00440 == sbi->security.next_off
- * need to zero [0x438-0x440)
- * if (next > used) {
- * u32 tozero = next - used;
- * zero "tozero" bytes from sbi->security.next_off - tozero
- */
- /* Format new security descriptor. */
- d_security->key.hash = hash_key.hash;
- d_security->key.sec_id = cpu_to_le32(sbi->security.next_id);
- d_security->off = cpu_to_le64(sbi->security.next_off);
- d_security->size = cpu_to_le32(new_sec_size);
- memcpy(d_security + 1, sd, size_sd);
- /* Write main SDS bucket. */
- err = ntfs_sb_write_run(sbi, &ni->file.run, sbi->security.next_off,
- d_security, aligned_sec_size, 0);
- if (err)
- goto out;
- mirr_off = sbi->security.next_off + SecurityDescriptorsBlockSize;
- new_sds_size = mirr_off + aligned_sec_size;
- if (new_sds_size > ni->vfs_inode.i_size) {
- err = attr_set_size(ni, ATTR_DATA, SDS_NAME,
- ARRAY_SIZE(SDS_NAME), &ni->file.run,
- new_sds_size, &new_sds_size, false, NULL);
- if (err)
- goto out;
- }
- /* Write copy SDS bucket. */
- err = ntfs_sb_write_run(sbi, &ni->file.run, mirr_off, d_security,
- aligned_sec_size, 0);
- if (err)
- goto out;
- /* Fill SII entry. */
- sii_e.de.view.data_off =
- cpu_to_le16(offsetof(struct NTFS_DE_SII, sec_hdr));
- sii_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
- sii_e.de.view.res = 0;
- sii_e.de.size = cpu_to_le16(sizeof(struct NTFS_DE_SII));
- sii_e.de.key_size = cpu_to_le16(sizeof(d_security->key.sec_id));
- sii_e.de.flags = 0;
- sii_e.de.res = 0;
- sii_e.sec_id = d_security->key.sec_id;
- memcpy(&sii_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
- err = indx_insert_entry(indx_sii, ni, &sii_e.de, NULL, NULL, 0);
- if (err)
- goto out;
- /* Fill SDH entry. */
- sdh_e.de.view.data_off =
- cpu_to_le16(offsetof(struct NTFS_DE_SDH, sec_hdr));
- sdh_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
- sdh_e.de.view.res = 0;
- sdh_e.de.size = cpu_to_le16(SIZEOF_SDH_DIRENTRY);
- sdh_e.de.key_size = cpu_to_le16(sizeof(sdh_e.key));
- sdh_e.de.flags = 0;
- sdh_e.de.res = 0;
- sdh_e.key.hash = d_security->key.hash;
- sdh_e.key.sec_id = d_security->key.sec_id;
- memcpy(&sdh_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
- sdh_e.magic[0] = cpu_to_le16('I');
- sdh_e.magic[1] = cpu_to_le16('I');
- fnd_clear(fnd_sdh);
- err = indx_insert_entry(indx_sdh, ni, &sdh_e.de, (void *)(size_t)1,
- fnd_sdh, 0);
- if (err)
- goto out;
- *security_id = d_security->key.sec_id;
- if (inserted)
- *inserted = true;
- /* Update Id and offset for next descriptor. */
- sbi->security.next_id += 1;
- sbi->security.next_off += aligned_sec_size;
- out:
- fnd_put(fnd_sdh);
- mark_inode_dirty(&ni->vfs_inode);
- ni_unlock(ni);
- kfree(d_security);
- return err;
- }
- /*
- * ntfs_reparse_init - Load and parse $Extend/$Reparse.
- */
- int ntfs_reparse_init(struct ntfs_sb_info *sbi)
- {
- int err;
- struct ntfs_inode *ni = sbi->reparse.ni;
- struct ntfs_index *indx = &sbi->reparse.index_r;
- struct ATTRIB *attr;
- struct ATTR_LIST_ENTRY *le;
- const struct INDEX_ROOT *root_r;
- if (!ni)
- return 0;
- le = NULL;
- attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SR_NAME,
- ARRAY_SIZE(SR_NAME), NULL, NULL);
- if (!attr) {
- err = -EINVAL;
- goto out;
- }
- root_r = resident_data(attr);
- if (root_r->type != ATTR_ZERO ||
- root_r->rule != NTFS_COLLATION_TYPE_UINTS) {
- err = -EINVAL;
- goto out;
- }
- err = indx_init(indx, sbi, attr, INDEX_MUTEX_SR);
- if (err)
- goto out;
- out:
- return err;
- }
- /*
- * ntfs_objid_init - Load and parse $Extend/$ObjId.
- */
- int ntfs_objid_init(struct ntfs_sb_info *sbi)
- {
- int err;
- struct ntfs_inode *ni = sbi->objid.ni;
- struct ntfs_index *indx = &sbi->objid.index_o;
- struct ATTRIB *attr;
- struct ATTR_LIST_ENTRY *le;
- const struct INDEX_ROOT *root;
- if (!ni)
- return 0;
- le = NULL;
- attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SO_NAME,
- ARRAY_SIZE(SO_NAME), NULL, NULL);
- if (!attr) {
- err = -EINVAL;
- goto out;
- }
- root = resident_data(attr);
- if (root->type != ATTR_ZERO ||
- root->rule != NTFS_COLLATION_TYPE_UINTS) {
- err = -EINVAL;
- goto out;
- }
- err = indx_init(indx, sbi, attr, INDEX_MUTEX_SO);
- if (err)
- goto out;
- out:
- return err;
- }
- int ntfs_objid_remove(struct ntfs_sb_info *sbi, struct GUID *guid)
- {
- int err;
- struct ntfs_inode *ni = sbi->objid.ni;
- struct ntfs_index *indx = &sbi->objid.index_o;
- if (!ni)
- return -EINVAL;
- mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_OBJID);
- err = indx_delete_entry(indx, ni, guid, sizeof(*guid), NULL);
- mark_inode_dirty(&ni->vfs_inode);
- ni_unlock(ni);
- return err;
- }
- int ntfs_insert_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
- const struct MFT_REF *ref)
- {
- int err;
- struct ntfs_inode *ni = sbi->reparse.ni;
- struct ntfs_index *indx = &sbi->reparse.index_r;
- struct NTFS_DE_R re;
- if (!ni)
- return -EINVAL;
- memset(&re, 0, sizeof(re));
- re.de.view.data_off = cpu_to_le16(offsetof(struct NTFS_DE_R, zero));
- re.de.size = cpu_to_le16(sizeof(struct NTFS_DE_R));
- re.de.key_size = cpu_to_le16(sizeof(re.key));
- re.key.ReparseTag = rtag;
- memcpy(&re.key.ref, ref, sizeof(*ref));
- mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_REPARSE);
- err = indx_insert_entry(indx, ni, &re.de, NULL, NULL, 0);
- mark_inode_dirty(&ni->vfs_inode);
- ni_unlock(ni);
- return err;
- }
- int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
- const struct MFT_REF *ref)
- {
- int err, diff;
- struct ntfs_inode *ni = sbi->reparse.ni;
- struct ntfs_index *indx = &sbi->reparse.index_r;
- struct ntfs_fnd *fnd = NULL;
- struct REPARSE_KEY rkey;
- struct NTFS_DE_R *re;
- struct INDEX_ROOT *root_r;
- if (!ni)
- return -EINVAL;
- rkey.ReparseTag = rtag;
- rkey.ref = *ref;
- mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_REPARSE);
- if (rtag) {
- err = indx_delete_entry(indx, ni, &rkey, sizeof(rkey), NULL);
- goto out1;
- }
- fnd = fnd_get();
- if (!fnd) {
- err = -ENOMEM;
- goto out1;
- }
- root_r = indx_get_root(indx, ni, NULL, NULL);
- if (!root_r) {
- err = -EINVAL;
- goto out;
- }
- /* 1 - forces to ignore rkey.ReparseTag when comparing keys. */
- err = indx_find(indx, ni, root_r, &rkey, sizeof(rkey), (void *)1, &diff,
- (struct NTFS_DE **)&re, fnd);
- if (err)
- goto out;
- if (memcmp(&re->key.ref, ref, sizeof(*ref))) {
- /* Impossible. Looks like volume corrupt? */
- goto out;
- }
- memcpy(&rkey, &re->key, sizeof(rkey));
- fnd_put(fnd);
- fnd = NULL;
- err = indx_delete_entry(indx, ni, &rkey, sizeof(rkey), NULL);
- if (err)
- goto out;
- out:
- fnd_put(fnd);
- out1:
- mark_inode_dirty(&ni->vfs_inode);
- ni_unlock(ni);
- return err;
- }
- static inline void ntfs_unmap_and_discard(struct ntfs_sb_info *sbi, CLST lcn,
- CLST len)
- {
- ntfs_unmap_meta(sbi->sb, lcn, len);
- ntfs_discard(sbi, lcn, len);
- }
- void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim)
- {
- CLST end, i, zone_len, zlen;
- struct wnd_bitmap *wnd = &sbi->used.bitmap;
- bool dirty = false;
- down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
- if (!wnd_is_used(wnd, lcn, len)) {
- /* mark volume as dirty out of wnd->rw_lock */
- dirty = true;
- end = lcn + len;
- len = 0;
- for (i = lcn; i < end; i++) {
- if (wnd_is_used(wnd, i, 1)) {
- if (!len)
- lcn = i;
- len += 1;
- continue;
- }
- if (!len)
- continue;
- if (trim)
- ntfs_unmap_and_discard(sbi, lcn, len);
- wnd_set_free(wnd, lcn, len);
- len = 0;
- }
- if (!len)
- goto out;
- }
- if (trim)
- ntfs_unmap_and_discard(sbi, lcn, len);
- wnd_set_free(wnd, lcn, len);
- /* append to MFT zone, if possible. */
- zone_len = wnd_zone_len(wnd);
- zlen = min(zone_len + len, sbi->zone_max);
- if (zlen == zone_len) {
- /* MFT zone already has maximum size. */
- } else if (!zone_len) {
- /* Create MFT zone only if 'zlen' is large enough. */
- if (zlen == sbi->zone_max)
- wnd_zone_set(wnd, lcn, zlen);
- } else {
- CLST zone_lcn = wnd_zone_bit(wnd);
- if (lcn + len == zone_lcn) {
- /* Append into head MFT zone. */
- wnd_zone_set(wnd, lcn, zlen);
- } else if (zone_lcn + zone_len == lcn) {
- /* Append into tail MFT zone. */
- wnd_zone_set(wnd, zone_lcn, zlen);
- }
- }
- out:
- up_write(&wnd->rw_lock);
- if (dirty)
- ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
- }
- /*
- * run_deallocate - Deallocate clusters.
- */
- int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
- bool trim)
- {
- CLST lcn, len;
- size_t idx = 0;
- while (run_get_entry(run, idx++, NULL, &lcn, &len)) {
- if (lcn == SPARSE_LCN)
- continue;
- mark_as_free_ex(sbi, lcn, len, trim);
- }
- return 0;
- }
- static inline bool name_has_forbidden_chars(const struct le_str *fname)
- {
- int i, ch;
- /* check for forbidden chars */
- for (i = 0; i < fname->len; ++i) {
- ch = le16_to_cpu(fname->name[i]);
- /* control chars */
- if (ch < 0x20)
- return true;
- switch (ch) {
- /* disallowed by Windows */
- case '\\':
- case '/':
- case ':':
- case '*':
- case '?':
- case '<':
- case '>':
- case '|':
- case '\"':
- return true;
- default:
- /* allowed char */
- break;
- }
- }
- /* file names cannot end with space or . */
- if (fname->len > 0) {
- ch = le16_to_cpu(fname->name[fname->len - 1]);
- if (ch == ' ' || ch == '.')
- return true;
- }
- return false;
- }
- static inline bool is_reserved_name(const struct ntfs_sb_info *sbi,
- const struct le_str *fname)
- {
- int port_digit;
- const __le16 *name = fname->name;
- int len = fname->len;
- const u16 *upcase = sbi->upcase;
- /* check for 3 chars reserved names (device names) */
- /* name by itself or with any extension is forbidden */
- if (len == 3 || (len > 3 && le16_to_cpu(name[3]) == '.'))
- if (!ntfs_cmp_names(name, 3, CON_NAME, 3, upcase, false) ||
- !ntfs_cmp_names(name, 3, NUL_NAME, 3, upcase, false) ||
- !ntfs_cmp_names(name, 3, AUX_NAME, 3, upcase, false) ||
- !ntfs_cmp_names(name, 3, PRN_NAME, 3, upcase, false))
- return true;
- /* check for 4 chars reserved names (port name followed by 1..9) */
- /* name by itself or with any extension is forbidden */
- if (len == 4 || (len > 4 && le16_to_cpu(name[4]) == '.')) {
- port_digit = le16_to_cpu(name[3]);
- if (port_digit >= '1' && port_digit <= '9')
- if (!ntfs_cmp_names(name, 3, COM_NAME, 3, upcase,
- false) ||
- !ntfs_cmp_names(name, 3, LPT_NAME, 3, upcase,
- false))
- return true;
- }
- return false;
- }
- /*
- * valid_windows_name - Check if a file name is valid in Windows.
- */
- bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str *fname)
- {
- return !name_has_forbidden_chars(fname) &&
- !is_reserved_name(sbi, fname);
- }
- /*
- * ntfs_set_label - updates current ntfs label.
- */
- int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len)
- {
- int err;
- struct ATTRIB *attr;
- u32 uni_bytes;
- struct ntfs_inode *ni = sbi->volume.ni;
- /* Allocate PATH_MAX bytes. */
- struct cpu_str *uni = __getname();
- if (!uni)
- return -ENOMEM;
- err = ntfs_nls_to_utf16(sbi, label, len, uni, (PATH_MAX - 2) / 2,
- UTF16_LITTLE_ENDIAN);
- if (err < 0)
- goto out;
- uni_bytes = uni->len * sizeof(u16);
- if (uni_bytes > NTFS_LABEL_MAX_LENGTH * sizeof(u16)) {
- ntfs_warn(sbi->sb, "new label is too long");
- err = -EFBIG;
- goto out;
- }
- ni_lock(ni);
- /* Ignore any errors. */
- ni_remove_attr(ni, ATTR_LABEL, NULL, 0, false, NULL);
- err = ni_insert_resident(ni, uni_bytes, ATTR_LABEL, NULL, 0, &attr,
- NULL, NULL);
- if (err < 0)
- goto unlock_out;
- /* write new label in on-disk struct. */
- memcpy(resident_data(attr), uni->name, uni_bytes);
- /* update cached value of current label. */
- if (len >= ARRAY_SIZE(sbi->volume.label))
- len = ARRAY_SIZE(sbi->volume.label) - 1;
- memcpy(sbi->volume.label, label, len);
- sbi->volume.label[len] = 0;
- mark_inode_dirty_sync(&ni->vfs_inode);
- unlock_out:
- ni_unlock(ni);
- if (!err)
- err = _ni_write_inode(&ni->vfs_inode, 0);
- out:
- __putname(uni);
- return err;
- }
|