namei.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/namei.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.h>
  24. #include <linux/ima.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/device_cgroup.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/posix_acl.h>
  34. #include <linux/hash.h>
  35. #include <linux/bitops.h>
  36. #include <asm/uaccess.h>
  37. #include "internal.h"
  38. #include "mount.h"
  39. /* [Feb-1997 T. Schoebel-Theuer]
  40. * Fundamental changes in the pathname lookup mechanisms (namei)
  41. * were necessary because of omirr. The reason is that omirr needs
  42. * to know the _real_ pathname, not the user-supplied one, in case
  43. * of symlinks (and also when transname replacements occur).
  44. *
  45. * The new code replaces the old recursive symlink resolution with
  46. * an iterative one (in case of non-nested symlink chains). It does
  47. * this with calls to <fs>_follow_link().
  48. * As a side effect, dir_namei(), _namei() and follow_link() are now
  49. * replaced with a single function lookup_dentry() that can handle all
  50. * the special cases of the former code.
  51. *
  52. * With the new dcache, the pathname is stored at each inode, at least as
  53. * long as the refcount of the inode is positive. As a side effect, the
  54. * size of the dcache depends on the inode cache and thus is dynamic.
  55. *
  56. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  57. * resolution to correspond with current state of the code.
  58. *
  59. * Note that the symlink resolution is not *completely* iterative.
  60. * There is still a significant amount of tail- and mid- recursion in
  61. * the algorithm. Also, note that <fs>_readlink() is not used in
  62. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  63. * may return different results than <fs>_follow_link(). Many virtual
  64. * filesystems (including /proc) exhibit this behavior.
  65. */
  66. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  67. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  68. * and the name already exists in form of a symlink, try to create the new
  69. * name indicated by the symlink. The old code always complained that the
  70. * name already exists, due to not following the symlink even if its target
  71. * is nonexistent. The new semantics affects also mknod() and link() when
  72. * the name is a symlink pointing to a non-existent name.
  73. *
  74. * I don't know which semantics is the right one, since I have no access
  75. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  76. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  77. * "old" one. Personally, I think the new semantics is much more logical.
  78. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  79. * file does succeed in both HP-UX and SunOs, but not in Solaris
  80. * and in the old Linux semantics.
  81. */
  82. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  83. * semantics. See the comments in "open_namei" and "do_link" below.
  84. *
  85. * [10-Sep-98 Alan Modra] Another symlink change.
  86. */
  87. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  88. * inside the path - always follow.
  89. * in the last component in creation/removal/renaming - never follow.
  90. * if LOOKUP_FOLLOW passed - follow.
  91. * if the pathname has trailing slashes - follow.
  92. * otherwise - don't follow.
  93. * (applied in that order).
  94. *
  95. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  96. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  97. * During the 2.4 we need to fix the userland stuff depending on it -
  98. * hopefully we will be able to get rid of that wart in 2.5. So far only
  99. * XEmacs seems to be relying on it...
  100. */
  101. /*
  102. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  103. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  104. * any extra contention...
  105. */
  106. /* In order to reduce some races, while at the same time doing additional
  107. * checking and hopefully speeding things up, we copy filenames to the
  108. * kernel data space before using them..
  109. *
  110. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  111. * PATH_MAX includes the nul terminator --RR.
  112. */
  113. #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
  114. struct filename *
  115. getname_flags(const char __user *filename, int flags, int *empty)
  116. {
  117. struct filename *result;
  118. char *kname;
  119. int len;
  120. result = audit_reusename(filename);
  121. if (result)
  122. return result;
  123. result = __getname();
  124. if (unlikely(!result))
  125. return ERR_PTR(-ENOMEM);
  126. /*
  127. * First, try to embed the struct filename inside the names_cache
  128. * allocation
  129. */
  130. kname = (char *)result->iname;
  131. result->name = kname;
  132. len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
  133. if (unlikely(len < 0)) {
  134. __putname(result);
  135. return ERR_PTR(len);
  136. }
  137. /*
  138. * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  139. * separate struct filename so we can dedicate the entire
  140. * names_cache allocation for the pathname, and re-do the copy from
  141. * userland.
  142. */
  143. if (unlikely(len == EMBEDDED_NAME_MAX)) {
  144. const size_t size = offsetof(struct filename, iname[1]);
  145. kname = (char *)result;
  146. /*
  147. * size is chosen that way we to guarantee that
  148. * result->iname[0] is within the same object and that
  149. * kname can't be equal to result->iname, no matter what.
  150. */
  151. result = kzalloc(size, GFP_KERNEL);
  152. if (unlikely(!result)) {
  153. __putname(kname);
  154. return ERR_PTR(-ENOMEM);
  155. }
  156. result->name = kname;
  157. len = strncpy_from_user(kname, filename, PATH_MAX);
  158. if (unlikely(len < 0)) {
  159. __putname(kname);
  160. kfree(result);
  161. return ERR_PTR(len);
  162. }
  163. if (unlikely(len == PATH_MAX)) {
  164. __putname(kname);
  165. kfree(result);
  166. return ERR_PTR(-ENAMETOOLONG);
  167. }
  168. }
  169. result->refcnt = 1;
  170. /* The empty path is special. */
  171. if (unlikely(!len)) {
  172. if (empty)
  173. *empty = 1;
  174. if (!(flags & LOOKUP_EMPTY)) {
  175. putname(result);
  176. return ERR_PTR(-ENOENT);
  177. }
  178. }
  179. result->uptr = filename;
  180. result->aname = NULL;
  181. audit_getname(result);
  182. return result;
  183. }
  184. struct filename *
  185. getname(const char __user * filename)
  186. {
  187. return getname_flags(filename, 0, NULL);
  188. }
  189. struct filename *
  190. getname_kernel(const char * filename)
  191. {
  192. struct filename *result;
  193. int len = strlen(filename) + 1;
  194. result = __getname();
  195. if (unlikely(!result))
  196. return ERR_PTR(-ENOMEM);
  197. if (len <= EMBEDDED_NAME_MAX) {
  198. result->name = (char *)result->iname;
  199. } else if (len <= PATH_MAX) {
  200. struct filename *tmp;
  201. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  202. if (unlikely(!tmp)) {
  203. __putname(result);
  204. return ERR_PTR(-ENOMEM);
  205. }
  206. tmp->name = (char *)result;
  207. result = tmp;
  208. } else {
  209. __putname(result);
  210. return ERR_PTR(-ENAMETOOLONG);
  211. }
  212. memcpy((char *)result->name, filename, len);
  213. result->uptr = NULL;
  214. result->aname = NULL;
  215. result->refcnt = 1;
  216. audit_getname(result);
  217. return result;
  218. }
  219. void putname(struct filename *name)
  220. {
  221. BUG_ON(name->refcnt <= 0);
  222. if (--name->refcnt > 0)
  223. return;
  224. if (name->name != name->iname) {
  225. __putname(name->name);
  226. kfree(name);
  227. } else
  228. __putname(name);
  229. }
  230. static int check_acl(struct inode *inode, int mask)
  231. {
  232. #ifdef CONFIG_FS_POSIX_ACL
  233. struct posix_acl *acl;
  234. if (mask & MAY_NOT_BLOCK) {
  235. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  236. if (!acl)
  237. return -EAGAIN;
  238. /* no ->get_acl() calls in RCU mode... */
  239. if (is_uncached_acl(acl))
  240. return -ECHILD;
  241. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  242. }
  243. acl = get_acl(inode, ACL_TYPE_ACCESS);
  244. if (IS_ERR(acl))
  245. return PTR_ERR(acl);
  246. if (acl) {
  247. int error = posix_acl_permission(inode, acl, mask);
  248. posix_acl_release(acl);
  249. return error;
  250. }
  251. #endif
  252. return -EAGAIN;
  253. }
  254. /*
  255. * This does the basic permission checking
  256. */
  257. static int acl_permission_check(struct inode *inode, int mask)
  258. {
  259. unsigned int mode = inode->i_mode;
  260. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  261. mode >>= 6;
  262. else {
  263. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  264. int error = check_acl(inode, mask);
  265. if (error != -EAGAIN)
  266. return error;
  267. }
  268. if (in_group_p(inode->i_gid))
  269. mode >>= 3;
  270. }
  271. /*
  272. * If the DACs are ok we don't need any capability check.
  273. */
  274. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  275. return 0;
  276. return -EACCES;
  277. }
  278. /**
  279. * generic_permission - check for access rights on a Posix-like filesystem
  280. * @inode: inode to check access rights for
  281. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  282. *
  283. * Used to check for read/write/execute permissions on a file.
  284. * We use "fsuid" for this, letting us set arbitrary permissions
  285. * for filesystem access without changing the "normal" uids which
  286. * are used for other things.
  287. *
  288. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  289. * request cannot be satisfied (eg. requires blocking or too much complexity).
  290. * It would then be called again in ref-walk mode.
  291. */
  292. int generic_permission(struct inode *inode, int mask)
  293. {
  294. int ret;
  295. /*
  296. * Do the basic permission checks.
  297. */
  298. ret = acl_permission_check(inode, mask);
  299. if (ret != -EACCES)
  300. return ret;
  301. if (S_ISDIR(inode->i_mode)) {
  302. /* DACs are overridable for directories */
  303. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  304. return 0;
  305. if (!(mask & MAY_WRITE))
  306. if (capable_wrt_inode_uidgid(inode,
  307. CAP_DAC_READ_SEARCH))
  308. return 0;
  309. return -EACCES;
  310. }
  311. /*
  312. * Read/write DACs are always overridable.
  313. * Executable DACs are overridable when there is
  314. * at least one exec bit set.
  315. */
  316. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  317. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  318. return 0;
  319. /*
  320. * Searching includes executable on directories, else just read.
  321. */
  322. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  323. if (mask == MAY_READ)
  324. if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
  325. return 0;
  326. return -EACCES;
  327. }
  328. EXPORT_SYMBOL(generic_permission);
  329. /*
  330. * We _really_ want to just do "generic_permission()" without
  331. * even looking at the inode->i_op values. So we keep a cache
  332. * flag in inode->i_opflags, that says "this has not special
  333. * permission function, use the fast case".
  334. */
  335. static inline int do_inode_permission(struct inode *inode, int mask)
  336. {
  337. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  338. if (likely(inode->i_op->permission))
  339. return inode->i_op->permission(inode, mask);
  340. /* This gets set once for the inode lifetime */
  341. spin_lock(&inode->i_lock);
  342. inode->i_opflags |= IOP_FASTPERM;
  343. spin_unlock(&inode->i_lock);
  344. }
  345. return generic_permission(inode, mask);
  346. }
  347. /**
  348. * __inode_permission - Check for access rights to a given inode
  349. * @inode: Inode to check permission on
  350. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  351. *
  352. * Check for read/write/execute permissions on an inode.
  353. *
  354. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  355. *
  356. * This does not check for a read-only file system. You probably want
  357. * inode_permission().
  358. */
  359. int __inode_permission(struct inode *inode, int mask)
  360. {
  361. int retval;
  362. if (unlikely(mask & MAY_WRITE)) {
  363. /*
  364. * Nobody gets write access to an immutable file.
  365. */
  366. if (IS_IMMUTABLE(inode))
  367. return -EACCES;
  368. }
  369. retval = do_inode_permission(inode, mask);
  370. if (retval)
  371. return retval;
  372. retval = devcgroup_inode_permission(inode, mask);
  373. if (retval)
  374. return retval;
  375. return security_inode_permission(inode, mask);
  376. }
  377. EXPORT_SYMBOL(__inode_permission);
  378. /**
  379. * sb_permission - Check superblock-level permissions
  380. * @sb: Superblock of inode to check permission on
  381. * @inode: Inode to check permission on
  382. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  383. *
  384. * Separate out file-system wide checks from inode-specific permission checks.
  385. */
  386. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  387. {
  388. if (unlikely(mask & MAY_WRITE)) {
  389. umode_t mode = inode->i_mode;
  390. /* Nobody gets write access to a read-only fs. */
  391. if ((sb->s_flags & MS_RDONLY) &&
  392. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  393. return -EROFS;
  394. }
  395. return 0;
  396. }
  397. /**
  398. * inode_permission - Check for access rights to a given inode
  399. * @inode: Inode to check permission on
  400. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  401. *
  402. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  403. * this, letting us set arbitrary permissions for filesystem access without
  404. * changing the "normal" UIDs which are used for other things.
  405. *
  406. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  407. */
  408. int inode_permission(struct inode *inode, int mask)
  409. {
  410. int retval;
  411. retval = sb_permission(inode->i_sb, inode, mask);
  412. if (retval)
  413. return retval;
  414. return __inode_permission(inode, mask);
  415. }
  416. EXPORT_SYMBOL(inode_permission);
  417. /**
  418. * path_get - get a reference to a path
  419. * @path: path to get the reference to
  420. *
  421. * Given a path increment the reference count to the dentry and the vfsmount.
  422. */
  423. void path_get(const struct path *path)
  424. {
  425. mntget(path->mnt);
  426. dget(path->dentry);
  427. }
  428. EXPORT_SYMBOL(path_get);
  429. /**
  430. * path_put - put a reference to a path
  431. * @path: path to put the reference to
  432. *
  433. * Given a path decrement the reference count to the dentry and the vfsmount.
  434. */
  435. void path_put(const struct path *path)
  436. {
  437. dput(path->dentry);
  438. mntput(path->mnt);
  439. }
  440. EXPORT_SYMBOL(path_put);
  441. #define EMBEDDED_LEVELS 2
  442. struct nameidata {
  443. struct path path;
  444. struct qstr last;
  445. struct path root;
  446. struct inode *inode; /* path.dentry.d_inode */
  447. unsigned int flags;
  448. unsigned seq, m_seq;
  449. int last_type;
  450. unsigned depth;
  451. int total_link_count;
  452. struct saved {
  453. struct path link;
  454. struct delayed_call done;
  455. const char *name;
  456. unsigned seq;
  457. } *stack, internal[EMBEDDED_LEVELS];
  458. struct filename *name;
  459. struct nameidata *saved;
  460. struct inode *link_inode;
  461. unsigned root_seq;
  462. int dfd;
  463. };
  464. static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
  465. {
  466. struct nameidata *old = current->nameidata;
  467. p->stack = p->internal;
  468. p->dfd = dfd;
  469. p->name = name;
  470. p->total_link_count = old ? old->total_link_count : 0;
  471. p->saved = old;
  472. current->nameidata = p;
  473. }
  474. static void restore_nameidata(void)
  475. {
  476. struct nameidata *now = current->nameidata, *old = now->saved;
  477. current->nameidata = old;
  478. if (old)
  479. old->total_link_count = now->total_link_count;
  480. if (now->stack != now->internal)
  481. kfree(now->stack);
  482. }
  483. static int __nd_alloc_stack(struct nameidata *nd)
  484. {
  485. struct saved *p;
  486. if (nd->flags & LOOKUP_RCU) {
  487. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  488. GFP_ATOMIC);
  489. if (unlikely(!p))
  490. return -ECHILD;
  491. } else {
  492. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  493. GFP_KERNEL);
  494. if (unlikely(!p))
  495. return -ENOMEM;
  496. }
  497. memcpy(p, nd->internal, sizeof(nd->internal));
  498. nd->stack = p;
  499. return 0;
  500. }
  501. /**
  502. * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
  503. * @path: nameidate to verify
  504. *
  505. * Rename can sometimes move a file or directory outside of a bind
  506. * mount, path_connected allows those cases to be detected.
  507. */
  508. static bool path_connected(const struct path *path)
  509. {
  510. struct vfsmount *mnt = path->mnt;
  511. /* Only bind mounts can have disconnected paths */
  512. if (mnt->mnt_root == mnt->mnt_sb->s_root)
  513. return true;
  514. return is_subdir(path->dentry, mnt->mnt_root);
  515. }
  516. static inline int nd_alloc_stack(struct nameidata *nd)
  517. {
  518. if (likely(nd->depth != EMBEDDED_LEVELS))
  519. return 0;
  520. if (likely(nd->stack != nd->internal))
  521. return 0;
  522. return __nd_alloc_stack(nd);
  523. }
  524. static void drop_links(struct nameidata *nd)
  525. {
  526. int i = nd->depth;
  527. while (i--) {
  528. struct saved *last = nd->stack + i;
  529. do_delayed_call(&last->done);
  530. clear_delayed_call(&last->done);
  531. }
  532. }
  533. static void terminate_walk(struct nameidata *nd)
  534. {
  535. drop_links(nd);
  536. if (!(nd->flags & LOOKUP_RCU)) {
  537. int i;
  538. path_put(&nd->path);
  539. for (i = 0; i < nd->depth; i++)
  540. path_put(&nd->stack[i].link);
  541. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  542. path_put(&nd->root);
  543. nd->root.mnt = NULL;
  544. }
  545. } else {
  546. nd->flags &= ~LOOKUP_RCU;
  547. if (!(nd->flags & LOOKUP_ROOT))
  548. nd->root.mnt = NULL;
  549. rcu_read_unlock();
  550. }
  551. nd->depth = 0;
  552. }
  553. /* path_put is needed afterwards regardless of success or failure */
  554. static bool legitimize_path(struct nameidata *nd,
  555. struct path *path, unsigned seq)
  556. {
  557. int res = __legitimize_mnt(path->mnt, nd->m_seq);
  558. if (unlikely(res)) {
  559. if (res > 0)
  560. path->mnt = NULL;
  561. path->dentry = NULL;
  562. return false;
  563. }
  564. if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
  565. path->dentry = NULL;
  566. return false;
  567. }
  568. return !read_seqcount_retry(&path->dentry->d_seq, seq);
  569. }
  570. static bool legitimize_links(struct nameidata *nd)
  571. {
  572. int i;
  573. for (i = 0; i < nd->depth; i++) {
  574. struct saved *last = nd->stack + i;
  575. if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
  576. drop_links(nd);
  577. nd->depth = i + 1;
  578. return false;
  579. }
  580. }
  581. return true;
  582. }
  583. /*
  584. * Path walking has 2 modes, rcu-walk and ref-walk (see
  585. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  586. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  587. * normal reference counts on dentries and vfsmounts to transition to ref-walk
  588. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  589. * got stuck, so ref-walk may continue from there. If this is not successful
  590. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  591. * to restart the path walk from the beginning in ref-walk mode.
  592. */
  593. /**
  594. * unlazy_walk - try to switch to ref-walk mode.
  595. * @nd: nameidata pathwalk data
  596. * @dentry: child of nd->path.dentry or NULL
  597. * @seq: seq number to check dentry against
  598. * Returns: 0 on success, -ECHILD on failure
  599. *
  600. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  601. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  602. * @nd or NULL. Must be called from rcu-walk context.
  603. * Nothing should touch nameidata between unlazy_walk() failure and
  604. * terminate_walk().
  605. */
  606. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
  607. {
  608. struct dentry *parent = nd->path.dentry;
  609. BUG_ON(!(nd->flags & LOOKUP_RCU));
  610. nd->flags &= ~LOOKUP_RCU;
  611. if (unlikely(!legitimize_links(nd)))
  612. goto out2;
  613. if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
  614. goto out2;
  615. if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
  616. goto out1;
  617. /*
  618. * For a negative lookup, the lookup sequence point is the parents
  619. * sequence point, and it only needs to revalidate the parent dentry.
  620. *
  621. * For a positive lookup, we need to move both the parent and the
  622. * dentry from the RCU domain to be properly refcounted. And the
  623. * sequence number in the dentry validates *both* dentry counters,
  624. * since we checked the sequence number of the parent after we got
  625. * the child sequence number. So we know the parent must still
  626. * be valid if the child sequence number is still valid.
  627. */
  628. if (!dentry) {
  629. if (read_seqcount_retry(&parent->d_seq, nd->seq))
  630. goto out;
  631. BUG_ON(nd->inode != parent->d_inode);
  632. } else {
  633. if (!lockref_get_not_dead(&dentry->d_lockref))
  634. goto out;
  635. if (read_seqcount_retry(&dentry->d_seq, seq))
  636. goto drop_dentry;
  637. }
  638. /*
  639. * Sequence counts matched. Now make sure that the root is
  640. * still valid and get it if required.
  641. */
  642. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  643. if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
  644. rcu_read_unlock();
  645. dput(dentry);
  646. return -ECHILD;
  647. }
  648. }
  649. rcu_read_unlock();
  650. return 0;
  651. drop_dentry:
  652. rcu_read_unlock();
  653. dput(dentry);
  654. goto drop_root_mnt;
  655. out2:
  656. nd->path.mnt = NULL;
  657. out1:
  658. nd->path.dentry = NULL;
  659. out:
  660. rcu_read_unlock();
  661. drop_root_mnt:
  662. if (!(nd->flags & LOOKUP_ROOT))
  663. nd->root.mnt = NULL;
  664. return -ECHILD;
  665. }
  666. static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
  667. {
  668. if (unlikely(!legitimize_path(nd, link, seq))) {
  669. drop_links(nd);
  670. nd->depth = 0;
  671. nd->flags &= ~LOOKUP_RCU;
  672. nd->path.mnt = NULL;
  673. nd->path.dentry = NULL;
  674. if (!(nd->flags & LOOKUP_ROOT))
  675. nd->root.mnt = NULL;
  676. rcu_read_unlock();
  677. } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
  678. return 0;
  679. }
  680. path_put(link);
  681. return -ECHILD;
  682. }
  683. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  684. {
  685. return dentry->d_op->d_revalidate(dentry, flags);
  686. }
  687. /**
  688. * complete_walk - successful completion of path walk
  689. * @nd: pointer nameidata
  690. *
  691. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  692. * Revalidate the final result, unless we'd already done that during
  693. * the path walk or the filesystem doesn't ask for it. Return 0 on
  694. * success, -error on failure. In case of failure caller does not
  695. * need to drop nd->path.
  696. */
  697. static int complete_walk(struct nameidata *nd)
  698. {
  699. struct dentry *dentry = nd->path.dentry;
  700. int status;
  701. if (nd->flags & LOOKUP_RCU) {
  702. if (!(nd->flags & LOOKUP_ROOT))
  703. nd->root.mnt = NULL;
  704. if (unlikely(unlazy_walk(nd, NULL, 0)))
  705. return -ECHILD;
  706. }
  707. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  708. return 0;
  709. if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
  710. return 0;
  711. status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
  712. if (status > 0)
  713. return 0;
  714. if (!status)
  715. status = -ESTALE;
  716. return status;
  717. }
  718. static void set_root(struct nameidata *nd)
  719. {
  720. struct fs_struct *fs = current->fs;
  721. if (nd->flags & LOOKUP_RCU) {
  722. unsigned seq;
  723. do {
  724. seq = read_seqcount_begin(&fs->seq);
  725. nd->root = fs->root;
  726. nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  727. } while (read_seqcount_retry(&fs->seq, seq));
  728. } else {
  729. get_fs_root(fs, &nd->root);
  730. }
  731. }
  732. static void path_put_conditional(struct path *path, struct nameidata *nd)
  733. {
  734. dput(path->dentry);
  735. if (path->mnt != nd->path.mnt)
  736. mntput(path->mnt);
  737. }
  738. static inline void path_to_nameidata(const struct path *path,
  739. struct nameidata *nd)
  740. {
  741. if (!(nd->flags & LOOKUP_RCU)) {
  742. dput(nd->path.dentry);
  743. if (nd->path.mnt != path->mnt)
  744. mntput(nd->path.mnt);
  745. }
  746. nd->path.mnt = path->mnt;
  747. nd->path.dentry = path->dentry;
  748. }
  749. static int nd_jump_root(struct nameidata *nd)
  750. {
  751. if (nd->flags & LOOKUP_RCU) {
  752. struct dentry *d;
  753. nd->path = nd->root;
  754. d = nd->path.dentry;
  755. nd->inode = d->d_inode;
  756. nd->seq = nd->root_seq;
  757. if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
  758. return -ECHILD;
  759. } else {
  760. path_put(&nd->path);
  761. nd->path = nd->root;
  762. path_get(&nd->path);
  763. nd->inode = nd->path.dentry->d_inode;
  764. }
  765. nd->flags |= LOOKUP_JUMPED;
  766. return 0;
  767. }
  768. /*
  769. * Helper to directly jump to a known parsed path from ->get_link,
  770. * caller must have taken a reference to path beforehand.
  771. */
  772. void nd_jump_link(struct path *path)
  773. {
  774. struct nameidata *nd = current->nameidata;
  775. path_put(&nd->path);
  776. nd->path = *path;
  777. nd->inode = nd->path.dentry->d_inode;
  778. nd->flags |= LOOKUP_JUMPED;
  779. }
  780. static inline void put_link(struct nameidata *nd)
  781. {
  782. struct saved *last = nd->stack + --nd->depth;
  783. do_delayed_call(&last->done);
  784. if (!(nd->flags & LOOKUP_RCU))
  785. path_put(&last->link);
  786. }
  787. int sysctl_protected_symlinks __read_mostly = 0;
  788. int sysctl_protected_hardlinks __read_mostly = 0;
  789. /**
  790. * may_follow_link - Check symlink following for unsafe situations
  791. * @nd: nameidata pathwalk data
  792. *
  793. * In the case of the sysctl_protected_symlinks sysctl being enabled,
  794. * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
  795. * in a sticky world-writable directory. This is to protect privileged
  796. * processes from failing races against path names that may change out
  797. * from under them by way of other users creating malicious symlinks.
  798. * It will permit symlinks to be followed only when outside a sticky
  799. * world-writable directory, or when the uid of the symlink and follower
  800. * match, or when the directory owner matches the symlink's owner.
  801. *
  802. * Returns 0 if following the symlink is allowed, -ve on error.
  803. */
  804. static inline int may_follow_link(struct nameidata *nd)
  805. {
  806. const struct inode *inode;
  807. const struct inode *parent;
  808. if (!sysctl_protected_symlinks)
  809. return 0;
  810. /* Allowed if owner and follower match. */
  811. inode = nd->link_inode;
  812. if (uid_eq(current_cred()->fsuid, inode->i_uid))
  813. return 0;
  814. /* Allowed if parent directory not sticky and world-writable. */
  815. parent = nd->inode;
  816. if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  817. return 0;
  818. /* Allowed if parent directory and link owner match. */
  819. if (uid_eq(parent->i_uid, inode->i_uid))
  820. return 0;
  821. if (nd->flags & LOOKUP_RCU)
  822. return -ECHILD;
  823. audit_log_link_denied("follow_link", &nd->stack[0].link);
  824. return -EACCES;
  825. }
  826. /**
  827. * safe_hardlink_source - Check for safe hardlink conditions
  828. * @inode: the source inode to hardlink from
  829. *
  830. * Return false if at least one of the following conditions:
  831. * - inode is not a regular file
  832. * - inode is setuid
  833. * - inode is setgid and group-exec
  834. * - access failure for read and write
  835. *
  836. * Otherwise returns true.
  837. */
  838. static bool safe_hardlink_source(struct inode *inode)
  839. {
  840. umode_t mode = inode->i_mode;
  841. /* Special files should not get pinned to the filesystem. */
  842. if (!S_ISREG(mode))
  843. return false;
  844. /* Setuid files should not get pinned to the filesystem. */
  845. if (mode & S_ISUID)
  846. return false;
  847. /* Executable setgid files should not get pinned to the filesystem. */
  848. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  849. return false;
  850. /* Hardlinking to unreadable or unwritable sources is dangerous. */
  851. if (inode_permission(inode, MAY_READ | MAY_WRITE))
  852. return false;
  853. return true;
  854. }
  855. /**
  856. * may_linkat - Check permissions for creating a hardlink
  857. * @link: the source to hardlink from
  858. *
  859. * Block hardlink when all of:
  860. * - sysctl_protected_hardlinks enabled
  861. * - fsuid does not match inode
  862. * - hardlink source is unsafe (see safe_hardlink_source() above)
  863. * - not CAP_FOWNER in a namespace with the inode owner uid mapped
  864. *
  865. * Returns 0 if successful, -ve on error.
  866. */
  867. static int may_linkat(struct path *link)
  868. {
  869. struct inode *inode;
  870. if (!sysctl_protected_hardlinks)
  871. return 0;
  872. inode = link->dentry->d_inode;
  873. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  874. * otherwise, it must be a safe source.
  875. */
  876. if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
  877. return 0;
  878. audit_log_link_denied("linkat", link);
  879. return -EPERM;
  880. }
  881. static __always_inline
  882. const char *get_link(struct nameidata *nd)
  883. {
  884. struct saved *last = nd->stack + nd->depth - 1;
  885. struct dentry *dentry = last->link.dentry;
  886. struct inode *inode = nd->link_inode;
  887. int error;
  888. const char *res;
  889. if (!(nd->flags & LOOKUP_RCU)) {
  890. touch_atime(&last->link);
  891. cond_resched();
  892. } else if (atime_needs_update(&last->link, inode)) {
  893. if (unlikely(unlazy_walk(nd, NULL, 0)))
  894. return ERR_PTR(-ECHILD);
  895. touch_atime(&last->link);
  896. }
  897. error = security_inode_follow_link(dentry, inode,
  898. nd->flags & LOOKUP_RCU);
  899. if (unlikely(error))
  900. return ERR_PTR(error);
  901. nd->last_type = LAST_BIND;
  902. res = inode->i_link;
  903. if (!res) {
  904. const char * (*get)(struct dentry *, struct inode *,
  905. struct delayed_call *);
  906. get = inode->i_op->get_link;
  907. if (nd->flags & LOOKUP_RCU) {
  908. res = get(NULL, inode, &last->done);
  909. if (res == ERR_PTR(-ECHILD)) {
  910. if (unlikely(unlazy_walk(nd, NULL, 0)))
  911. return ERR_PTR(-ECHILD);
  912. res = get(dentry, inode, &last->done);
  913. }
  914. } else {
  915. res = get(dentry, inode, &last->done);
  916. }
  917. if (IS_ERR_OR_NULL(res))
  918. return res;
  919. }
  920. if (*res == '/') {
  921. if (!nd->root.mnt)
  922. set_root(nd);
  923. if (unlikely(nd_jump_root(nd)))
  924. return ERR_PTR(-ECHILD);
  925. while (unlikely(*++res == '/'))
  926. ;
  927. }
  928. if (!*res)
  929. res = NULL;
  930. return res;
  931. }
  932. /*
  933. * follow_up - Find the mountpoint of path's vfsmount
  934. *
  935. * Given a path, find the mountpoint of its source file system.
  936. * Replace @path with the path of the mountpoint in the parent mount.
  937. * Up is towards /.
  938. *
  939. * Return 1 if we went up a level and 0 if we were already at the
  940. * root.
  941. */
  942. int follow_up(struct path *path)
  943. {
  944. struct mount *mnt = real_mount(path->mnt);
  945. struct mount *parent;
  946. struct dentry *mountpoint;
  947. read_seqlock_excl(&mount_lock);
  948. parent = mnt->mnt_parent;
  949. if (parent == mnt) {
  950. read_sequnlock_excl(&mount_lock);
  951. return 0;
  952. }
  953. mntget(&parent->mnt);
  954. mountpoint = dget(mnt->mnt_mountpoint);
  955. read_sequnlock_excl(&mount_lock);
  956. dput(path->dentry);
  957. path->dentry = mountpoint;
  958. mntput(path->mnt);
  959. path->mnt = &parent->mnt;
  960. return 1;
  961. }
  962. EXPORT_SYMBOL(follow_up);
  963. /*
  964. * Perform an automount
  965. * - return -EISDIR to tell follow_managed() to stop and return the path we
  966. * were called with.
  967. */
  968. static int follow_automount(struct path *path, struct nameidata *nd,
  969. bool *need_mntput)
  970. {
  971. struct vfsmount *mnt;
  972. int err;
  973. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  974. return -EREMOTE;
  975. /* We don't want to mount if someone's just doing a stat -
  976. * unless they're stat'ing a directory and appended a '/' to
  977. * the name.
  978. *
  979. * We do, however, want to mount if someone wants to open or
  980. * create a file of any type under the mountpoint, wants to
  981. * traverse through the mountpoint or wants to open the
  982. * mounted directory. Also, autofs may mark negative dentries
  983. * as being automount points. These will need the attentions
  984. * of the daemon to instantiate them before they can be used.
  985. */
  986. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  987. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  988. path->dentry->d_inode)
  989. return -EISDIR;
  990. nd->total_link_count++;
  991. if (nd->total_link_count >= 40)
  992. return -ELOOP;
  993. mnt = path->dentry->d_op->d_automount(path);
  994. if (IS_ERR(mnt)) {
  995. /*
  996. * The filesystem is allowed to return -EISDIR here to indicate
  997. * it doesn't want to automount. For instance, autofs would do
  998. * this so that its userspace daemon can mount on this dentry.
  999. *
  1000. * However, we can only permit this if it's a terminal point in
  1001. * the path being looked up; if it wasn't then the remainder of
  1002. * the path is inaccessible and we should say so.
  1003. */
  1004. if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
  1005. return -EREMOTE;
  1006. return PTR_ERR(mnt);
  1007. }
  1008. if (!mnt) /* mount collision */
  1009. return 0;
  1010. if (!*need_mntput) {
  1011. /* lock_mount() may release path->mnt on error */
  1012. mntget(path->mnt);
  1013. *need_mntput = true;
  1014. }
  1015. err = finish_automount(mnt, path);
  1016. switch (err) {
  1017. case -EBUSY:
  1018. /* Someone else made a mount here whilst we were busy */
  1019. return 0;
  1020. case 0:
  1021. path_put(path);
  1022. path->mnt = mnt;
  1023. path->dentry = dget(mnt->mnt_root);
  1024. return 0;
  1025. default:
  1026. return err;
  1027. }
  1028. }
  1029. /*
  1030. * Handle a dentry that is managed in some way.
  1031. * - Flagged for transit management (autofs)
  1032. * - Flagged as mountpoint
  1033. * - Flagged as automount point
  1034. *
  1035. * This may only be called in refwalk mode.
  1036. *
  1037. * Serialization is taken care of in namespace.c
  1038. */
  1039. static int follow_managed(struct path *path, struct nameidata *nd)
  1040. {
  1041. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  1042. unsigned managed;
  1043. bool need_mntput = false;
  1044. int ret = 0;
  1045. /* Given that we're not holding a lock here, we retain the value in a
  1046. * local variable for each dentry as we look at it so that we don't see
  1047. * the components of that value change under us */
  1048. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1049. managed &= DCACHE_MANAGED_DENTRY,
  1050. unlikely(managed != 0)) {
  1051. /* Allow the filesystem to manage the transit without i_mutex
  1052. * being held. */
  1053. if (managed & DCACHE_MANAGE_TRANSIT) {
  1054. BUG_ON(!path->dentry->d_op);
  1055. BUG_ON(!path->dentry->d_op->d_manage);
  1056. ret = path->dentry->d_op->d_manage(path->dentry, false);
  1057. if (ret < 0)
  1058. break;
  1059. }
  1060. /* Transit to a mounted filesystem. */
  1061. if (managed & DCACHE_MOUNTED) {
  1062. struct vfsmount *mounted = lookup_mnt(path);
  1063. if (mounted) {
  1064. dput(path->dentry);
  1065. if (need_mntput)
  1066. mntput(path->mnt);
  1067. path->mnt = mounted;
  1068. path->dentry = dget(mounted->mnt_root);
  1069. need_mntput = true;
  1070. continue;
  1071. }
  1072. /* Something is mounted on this dentry in another
  1073. * namespace and/or whatever was mounted there in this
  1074. * namespace got unmounted before lookup_mnt() could
  1075. * get it */
  1076. }
  1077. /* Handle an automount point */
  1078. if (managed & DCACHE_NEED_AUTOMOUNT) {
  1079. ret = follow_automount(path, nd, &need_mntput);
  1080. if (ret < 0)
  1081. break;
  1082. continue;
  1083. }
  1084. /* We didn't change the current path point */
  1085. break;
  1086. }
  1087. if (need_mntput && path->mnt == mnt)
  1088. mntput(path->mnt);
  1089. if (ret == -EISDIR || !ret)
  1090. ret = 1;
  1091. if (need_mntput)
  1092. nd->flags |= LOOKUP_JUMPED;
  1093. if (unlikely(ret < 0))
  1094. path_put_conditional(path, nd);
  1095. return ret;
  1096. }
  1097. int follow_down_one(struct path *path)
  1098. {
  1099. struct vfsmount *mounted;
  1100. mounted = lookup_mnt(path);
  1101. if (mounted) {
  1102. dput(path->dentry);
  1103. mntput(path->mnt);
  1104. path->mnt = mounted;
  1105. path->dentry = dget(mounted->mnt_root);
  1106. return 1;
  1107. }
  1108. return 0;
  1109. }
  1110. EXPORT_SYMBOL(follow_down_one);
  1111. static inline int managed_dentry_rcu(struct dentry *dentry)
  1112. {
  1113. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  1114. dentry->d_op->d_manage(dentry, true) : 0;
  1115. }
  1116. /*
  1117. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  1118. * we meet a managed dentry that would need blocking.
  1119. */
  1120. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  1121. struct inode **inode, unsigned *seqp)
  1122. {
  1123. for (;;) {
  1124. struct mount *mounted;
  1125. /*
  1126. * Don't forget we might have a non-mountpoint managed dentry
  1127. * that wants to block transit.
  1128. */
  1129. switch (managed_dentry_rcu(path->dentry)) {
  1130. case -ECHILD:
  1131. default:
  1132. return false;
  1133. case -EISDIR:
  1134. return true;
  1135. case 0:
  1136. break;
  1137. }
  1138. if (!d_mountpoint(path->dentry))
  1139. return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1140. mounted = __lookup_mnt(path->mnt, path->dentry);
  1141. if (!mounted)
  1142. break;
  1143. path->mnt = &mounted->mnt;
  1144. path->dentry = mounted->mnt.mnt_root;
  1145. nd->flags |= LOOKUP_JUMPED;
  1146. *seqp = read_seqcount_begin(&path->dentry->d_seq);
  1147. /*
  1148. * Update the inode too. We don't need to re-check the
  1149. * dentry sequence number here after this d_inode read,
  1150. * because a mount-point is always pinned.
  1151. */
  1152. *inode = path->dentry->d_inode;
  1153. }
  1154. return !read_seqretry(&mount_lock, nd->m_seq) &&
  1155. !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1156. }
  1157. static int follow_dotdot_rcu(struct nameidata *nd)
  1158. {
  1159. struct inode *inode = nd->inode;
  1160. while (1) {
  1161. if (path_equal(&nd->path, &nd->root))
  1162. break;
  1163. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1164. struct dentry *old = nd->path.dentry;
  1165. struct dentry *parent = old->d_parent;
  1166. unsigned seq;
  1167. inode = parent->d_inode;
  1168. seq = read_seqcount_begin(&parent->d_seq);
  1169. if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
  1170. return -ECHILD;
  1171. nd->path.dentry = parent;
  1172. nd->seq = seq;
  1173. if (unlikely(!path_connected(&nd->path)))
  1174. return -ENOENT;
  1175. break;
  1176. } else {
  1177. struct mount *mnt = real_mount(nd->path.mnt);
  1178. struct mount *mparent = mnt->mnt_parent;
  1179. struct dentry *mountpoint = mnt->mnt_mountpoint;
  1180. struct inode *inode2 = mountpoint->d_inode;
  1181. unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
  1182. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1183. return -ECHILD;
  1184. if (&mparent->mnt == nd->path.mnt)
  1185. break;
  1186. /* we know that mountpoint was pinned */
  1187. nd->path.dentry = mountpoint;
  1188. nd->path.mnt = &mparent->mnt;
  1189. inode = inode2;
  1190. nd->seq = seq;
  1191. }
  1192. }
  1193. while (unlikely(d_mountpoint(nd->path.dentry))) {
  1194. struct mount *mounted;
  1195. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1196. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1197. return -ECHILD;
  1198. if (!mounted)
  1199. break;
  1200. nd->path.mnt = &mounted->mnt;
  1201. nd->path.dentry = mounted->mnt.mnt_root;
  1202. inode = nd->path.dentry->d_inode;
  1203. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1204. }
  1205. nd->inode = inode;
  1206. return 0;
  1207. }
  1208. /*
  1209. * Follow down to the covering mount currently visible to userspace. At each
  1210. * point, the filesystem owning that dentry may be queried as to whether the
  1211. * caller is permitted to proceed or not.
  1212. */
  1213. int follow_down(struct path *path)
  1214. {
  1215. unsigned managed;
  1216. int ret;
  1217. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1218. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1219. /* Allow the filesystem to manage the transit without i_mutex
  1220. * being held.
  1221. *
  1222. * We indicate to the filesystem if someone is trying to mount
  1223. * something here. This gives autofs the chance to deny anyone
  1224. * other than its daemon the right to mount on its
  1225. * superstructure.
  1226. *
  1227. * The filesystem may sleep at this point.
  1228. */
  1229. if (managed & DCACHE_MANAGE_TRANSIT) {
  1230. BUG_ON(!path->dentry->d_op);
  1231. BUG_ON(!path->dentry->d_op->d_manage);
  1232. ret = path->dentry->d_op->d_manage(
  1233. path->dentry, false);
  1234. if (ret < 0)
  1235. return ret == -EISDIR ? 0 : ret;
  1236. }
  1237. /* Transit to a mounted filesystem. */
  1238. if (managed & DCACHE_MOUNTED) {
  1239. struct vfsmount *mounted = lookup_mnt(path);
  1240. if (!mounted)
  1241. break;
  1242. dput(path->dentry);
  1243. mntput(path->mnt);
  1244. path->mnt = mounted;
  1245. path->dentry = dget(mounted->mnt_root);
  1246. continue;
  1247. }
  1248. /* Don't handle automount points here */
  1249. break;
  1250. }
  1251. return 0;
  1252. }
  1253. EXPORT_SYMBOL(follow_down);
  1254. /*
  1255. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1256. */
  1257. static void follow_mount(struct path *path)
  1258. {
  1259. while (d_mountpoint(path->dentry)) {
  1260. struct vfsmount *mounted = lookup_mnt(path);
  1261. if (!mounted)
  1262. break;
  1263. dput(path->dentry);
  1264. mntput(path->mnt);
  1265. path->mnt = mounted;
  1266. path->dentry = dget(mounted->mnt_root);
  1267. }
  1268. }
  1269. static int follow_dotdot(struct nameidata *nd)
  1270. {
  1271. while(1) {
  1272. struct dentry *old = nd->path.dentry;
  1273. if (nd->path.dentry == nd->root.dentry &&
  1274. nd->path.mnt == nd->root.mnt) {
  1275. break;
  1276. }
  1277. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1278. /* rare case of legitimate dget_parent()... */
  1279. nd->path.dentry = dget_parent(nd->path.dentry);
  1280. dput(old);
  1281. if (unlikely(!path_connected(&nd->path)))
  1282. return -ENOENT;
  1283. break;
  1284. }
  1285. if (!follow_up(&nd->path))
  1286. break;
  1287. }
  1288. follow_mount(&nd->path);
  1289. nd->inode = nd->path.dentry->d_inode;
  1290. return 0;
  1291. }
  1292. /*
  1293. * This looks up the name in dcache, possibly revalidates the old dentry and
  1294. * allocates a new one if not found or not valid. In the need_lookup argument
  1295. * returns whether i_op->lookup is necessary.
  1296. */
  1297. static struct dentry *lookup_dcache(const struct qstr *name,
  1298. struct dentry *dir,
  1299. unsigned int flags)
  1300. {
  1301. struct dentry *dentry;
  1302. int error;
  1303. dentry = d_lookup(dir, name);
  1304. if (dentry) {
  1305. if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
  1306. error = d_revalidate(dentry, flags);
  1307. if (unlikely(error <= 0)) {
  1308. if (!error)
  1309. d_invalidate(dentry);
  1310. dput(dentry);
  1311. return ERR_PTR(error);
  1312. }
  1313. }
  1314. }
  1315. return dentry;
  1316. }
  1317. /*
  1318. * Call i_op->lookup on the dentry. The dentry must be negative and
  1319. * unhashed.
  1320. *
  1321. * dir->d_inode->i_mutex must be held
  1322. */
  1323. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1324. unsigned int flags)
  1325. {
  1326. struct dentry *old;
  1327. /* Don't create child dentry for a dead directory. */
  1328. if (unlikely(IS_DEADDIR(dir))) {
  1329. dput(dentry);
  1330. return ERR_PTR(-ENOENT);
  1331. }
  1332. old = dir->i_op->lookup(dir, dentry, flags);
  1333. if (unlikely(old)) {
  1334. dput(dentry);
  1335. dentry = old;
  1336. }
  1337. return dentry;
  1338. }
  1339. static struct dentry *__lookup_hash(const struct qstr *name,
  1340. struct dentry *base, unsigned int flags)
  1341. {
  1342. struct dentry *dentry = lookup_dcache(name, base, flags);
  1343. if (dentry)
  1344. return dentry;
  1345. dentry = d_alloc(base, name);
  1346. if (unlikely(!dentry))
  1347. return ERR_PTR(-ENOMEM);
  1348. return lookup_real(base->d_inode, dentry, flags);
  1349. }
  1350. static int lookup_fast(struct nameidata *nd,
  1351. struct path *path, struct inode **inode,
  1352. unsigned *seqp)
  1353. {
  1354. struct vfsmount *mnt = nd->path.mnt;
  1355. struct dentry *dentry, *parent = nd->path.dentry;
  1356. int status = 1;
  1357. int err;
  1358. /*
  1359. * Rename seqlock is not required here because in the off chance
  1360. * of a false negative due to a concurrent rename, the caller is
  1361. * going to fall back to non-racy lookup.
  1362. */
  1363. if (nd->flags & LOOKUP_RCU) {
  1364. unsigned seq;
  1365. bool negative;
  1366. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1367. if (unlikely(!dentry)) {
  1368. if (unlazy_walk(nd, NULL, 0))
  1369. return -ECHILD;
  1370. return 0;
  1371. }
  1372. /*
  1373. * This sequence count validates that the inode matches
  1374. * the dentry name information from lookup.
  1375. */
  1376. *inode = d_backing_inode(dentry);
  1377. negative = d_is_negative(dentry);
  1378. if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
  1379. return -ECHILD;
  1380. /*
  1381. * This sequence count validates that the parent had no
  1382. * changes while we did the lookup of the dentry above.
  1383. *
  1384. * The memory barrier in read_seqcount_begin of child is
  1385. * enough, we can use __read_seqcount_retry here.
  1386. */
  1387. if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
  1388. return -ECHILD;
  1389. *seqp = seq;
  1390. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
  1391. status = d_revalidate(dentry, nd->flags);
  1392. if (unlikely(status <= 0)) {
  1393. if (unlazy_walk(nd, dentry, seq))
  1394. return -ECHILD;
  1395. if (status == -ECHILD)
  1396. status = d_revalidate(dentry, nd->flags);
  1397. } else {
  1398. /*
  1399. * Note: do negative dentry check after revalidation in
  1400. * case that drops it.
  1401. */
  1402. if (unlikely(negative))
  1403. return -ENOENT;
  1404. path->mnt = mnt;
  1405. path->dentry = dentry;
  1406. if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
  1407. return 1;
  1408. if (unlazy_walk(nd, dentry, seq))
  1409. return -ECHILD;
  1410. }
  1411. } else {
  1412. dentry = __d_lookup(parent, &nd->last);
  1413. if (unlikely(!dentry))
  1414. return 0;
  1415. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
  1416. status = d_revalidate(dentry, nd->flags);
  1417. }
  1418. if (unlikely(status <= 0)) {
  1419. if (!status)
  1420. d_invalidate(dentry);
  1421. dput(dentry);
  1422. return status;
  1423. }
  1424. if (unlikely(d_is_negative(dentry))) {
  1425. dput(dentry);
  1426. return -ENOENT;
  1427. }
  1428. path->mnt = mnt;
  1429. path->dentry = dentry;
  1430. err = follow_managed(path, nd);
  1431. if (likely(err > 0))
  1432. *inode = d_backing_inode(path->dentry);
  1433. return err;
  1434. }
  1435. /* Fast lookup failed, do it the slow way */
  1436. static struct dentry *lookup_slow(const struct qstr *name,
  1437. struct dentry *dir,
  1438. unsigned int flags)
  1439. {
  1440. struct dentry *dentry = ERR_PTR(-ENOENT), *old;
  1441. struct inode *inode = dir->d_inode;
  1442. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1443. inode_lock_shared(inode);
  1444. /* Don't go there if it's already dead */
  1445. if (unlikely(IS_DEADDIR(inode)))
  1446. goto out;
  1447. again:
  1448. dentry = d_alloc_parallel(dir, name, &wq);
  1449. if (IS_ERR(dentry))
  1450. goto out;
  1451. if (unlikely(!d_in_lookup(dentry))) {
  1452. if ((dentry->d_flags & DCACHE_OP_REVALIDATE) &&
  1453. !(flags & LOOKUP_NO_REVAL)) {
  1454. int error = d_revalidate(dentry, flags);
  1455. if (unlikely(error <= 0)) {
  1456. if (!error) {
  1457. d_invalidate(dentry);
  1458. dput(dentry);
  1459. goto again;
  1460. }
  1461. dput(dentry);
  1462. dentry = ERR_PTR(error);
  1463. }
  1464. }
  1465. } else {
  1466. old = inode->i_op->lookup(inode, dentry, flags);
  1467. d_lookup_done(dentry);
  1468. if (unlikely(old)) {
  1469. dput(dentry);
  1470. dentry = old;
  1471. }
  1472. }
  1473. out:
  1474. inode_unlock_shared(inode);
  1475. return dentry;
  1476. }
  1477. static inline int may_lookup(struct nameidata *nd)
  1478. {
  1479. if (nd->flags & LOOKUP_RCU) {
  1480. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1481. if (err != -ECHILD)
  1482. return err;
  1483. if (unlazy_walk(nd, NULL, 0))
  1484. return -ECHILD;
  1485. }
  1486. return inode_permission(nd->inode, MAY_EXEC);
  1487. }
  1488. static inline int handle_dots(struct nameidata *nd, int type)
  1489. {
  1490. if (type == LAST_DOTDOT) {
  1491. if (!nd->root.mnt)
  1492. set_root(nd);
  1493. if (nd->flags & LOOKUP_RCU) {
  1494. return follow_dotdot_rcu(nd);
  1495. } else
  1496. return follow_dotdot(nd);
  1497. }
  1498. return 0;
  1499. }
  1500. static int pick_link(struct nameidata *nd, struct path *link,
  1501. struct inode *inode, unsigned seq)
  1502. {
  1503. int error;
  1504. struct saved *last;
  1505. if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
  1506. path_to_nameidata(link, nd);
  1507. return -ELOOP;
  1508. }
  1509. if (!(nd->flags & LOOKUP_RCU)) {
  1510. if (link->mnt == nd->path.mnt)
  1511. mntget(link->mnt);
  1512. }
  1513. error = nd_alloc_stack(nd);
  1514. if (unlikely(error)) {
  1515. if (error == -ECHILD) {
  1516. if (unlikely(unlazy_link(nd, link, seq)))
  1517. return -ECHILD;
  1518. error = nd_alloc_stack(nd);
  1519. }
  1520. if (error) {
  1521. path_put(link);
  1522. return error;
  1523. }
  1524. }
  1525. last = nd->stack + nd->depth++;
  1526. last->link = *link;
  1527. clear_delayed_call(&last->done);
  1528. nd->link_inode = inode;
  1529. last->seq = seq;
  1530. return 1;
  1531. }
  1532. /*
  1533. * Do we need to follow links? We _really_ want to be able
  1534. * to do this check without having to look at inode->i_op,
  1535. * so we keep a cache of "no, this doesn't need follow_link"
  1536. * for the common case.
  1537. */
  1538. static inline int should_follow_link(struct nameidata *nd, struct path *link,
  1539. int follow,
  1540. struct inode *inode, unsigned seq)
  1541. {
  1542. if (likely(!d_is_symlink(link->dentry)))
  1543. return 0;
  1544. if (!follow)
  1545. return 0;
  1546. /* make sure that d_is_symlink above matches inode */
  1547. if (nd->flags & LOOKUP_RCU) {
  1548. if (read_seqcount_retry(&link->dentry->d_seq, seq))
  1549. return -ECHILD;
  1550. }
  1551. return pick_link(nd, link, inode, seq);
  1552. }
  1553. enum {WALK_GET = 1, WALK_PUT = 2};
  1554. static int walk_component(struct nameidata *nd, int flags)
  1555. {
  1556. struct path path;
  1557. struct inode *inode;
  1558. unsigned seq;
  1559. int err;
  1560. /*
  1561. * "." and ".." are special - ".." especially so because it has
  1562. * to be able to know about the current root directory and
  1563. * parent relationships.
  1564. */
  1565. if (unlikely(nd->last_type != LAST_NORM)) {
  1566. err = handle_dots(nd, nd->last_type);
  1567. if (flags & WALK_PUT)
  1568. put_link(nd);
  1569. return err;
  1570. }
  1571. err = lookup_fast(nd, &path, &inode, &seq);
  1572. if (unlikely(err <= 0)) {
  1573. if (err < 0)
  1574. return err;
  1575. path.dentry = lookup_slow(&nd->last, nd->path.dentry,
  1576. nd->flags);
  1577. if (IS_ERR(path.dentry))
  1578. return PTR_ERR(path.dentry);
  1579. path.mnt = nd->path.mnt;
  1580. err = follow_managed(&path, nd);
  1581. if (unlikely(err < 0))
  1582. return err;
  1583. if (unlikely(d_is_negative(path.dentry))) {
  1584. path_to_nameidata(&path, nd);
  1585. return -ENOENT;
  1586. }
  1587. seq = 0; /* we are already out of RCU mode */
  1588. inode = d_backing_inode(path.dentry);
  1589. }
  1590. if (flags & WALK_PUT)
  1591. put_link(nd);
  1592. err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
  1593. if (unlikely(err))
  1594. return err;
  1595. path_to_nameidata(&path, nd);
  1596. nd->inode = inode;
  1597. nd->seq = seq;
  1598. return 0;
  1599. }
  1600. /*
  1601. * We can do the critical dentry name comparison and hashing
  1602. * operations one word at a time, but we are limited to:
  1603. *
  1604. * - Architectures with fast unaligned word accesses. We could
  1605. * do a "get_unaligned()" if this helps and is sufficiently
  1606. * fast.
  1607. *
  1608. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1609. * do not trap on the (extremely unlikely) case of a page
  1610. * crossing operation.
  1611. *
  1612. * - Furthermore, we need an efficient 64-bit compile for the
  1613. * 64-bit case in order to generate the "number of bytes in
  1614. * the final mask". Again, that could be replaced with a
  1615. * efficient population count instruction or similar.
  1616. */
  1617. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1618. #include <asm/word-at-a-time.h>
  1619. #ifdef HASH_MIX
  1620. /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
  1621. #elif defined(CONFIG_64BIT)
  1622. /*
  1623. * Register pressure in the mixing function is an issue, particularly
  1624. * on 32-bit x86, but almost any function requires one state value and
  1625. * one temporary. Instead, use a function designed for two state values
  1626. * and no temporaries.
  1627. *
  1628. * This function cannot create a collision in only two iterations, so
  1629. * we have two iterations to achieve avalanche. In those two iterations,
  1630. * we have six layers of mixing, which is enough to spread one bit's
  1631. * influence out to 2^6 = 64 state bits.
  1632. *
  1633. * Rotate constants are scored by considering either 64 one-bit input
  1634. * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
  1635. * probability of that delta causing a change to each of the 128 output
  1636. * bits, using a sample of random initial states.
  1637. *
  1638. * The Shannon entropy of the computed probabilities is then summed
  1639. * to produce a score. Ideally, any input change has a 50% chance of
  1640. * toggling any given output bit.
  1641. *
  1642. * Mixing scores (in bits) for (12,45):
  1643. * Input delta: 1-bit 2-bit
  1644. * 1 round: 713.3 42542.6
  1645. * 2 rounds: 2753.7 140389.8
  1646. * 3 rounds: 5954.1 233458.2
  1647. * 4 rounds: 7862.6 256672.2
  1648. * Perfect: 8192 258048
  1649. * (64*128) (64*63/2 * 128)
  1650. */
  1651. #define HASH_MIX(x, y, a) \
  1652. ( x ^= (a), \
  1653. y ^= x, x = rol64(x,12),\
  1654. x += y, y = rol64(y,45),\
  1655. y *= 9 )
  1656. /*
  1657. * Fold two longs into one 32-bit hash value. This must be fast, but
  1658. * latency isn't quite as critical, as there is a fair bit of additional
  1659. * work done before the hash value is used.
  1660. */
  1661. static inline unsigned int fold_hash(unsigned long x, unsigned long y)
  1662. {
  1663. y ^= x * GOLDEN_RATIO_64;
  1664. y *= GOLDEN_RATIO_64;
  1665. return y >> 32;
  1666. }
  1667. #else /* 32-bit case */
  1668. /*
  1669. * Mixing scores (in bits) for (7,20):
  1670. * Input delta: 1-bit 2-bit
  1671. * 1 round: 330.3 9201.6
  1672. * 2 rounds: 1246.4 25475.4
  1673. * 3 rounds: 1907.1 31295.1
  1674. * 4 rounds: 2042.3 31718.6
  1675. * Perfect: 2048 31744
  1676. * (32*64) (32*31/2 * 64)
  1677. */
  1678. #define HASH_MIX(x, y, a) \
  1679. ( x ^= (a), \
  1680. y ^= x, x = rol32(x, 7),\
  1681. x += y, y = rol32(y,20),\
  1682. y *= 9 )
  1683. static inline unsigned int fold_hash(unsigned long x, unsigned long y)
  1684. {
  1685. /* Use arch-optimized multiply if one exists */
  1686. return __hash_32(y ^ __hash_32(x));
  1687. }
  1688. #endif
  1689. /*
  1690. * Return the hash of a string of known length. This is carfully
  1691. * designed to match hash_name(), which is the more critical function.
  1692. * In particular, we must end by hashing a final word containing 0..7
  1693. * payload bytes, to match the way that hash_name() iterates until it
  1694. * finds the delimiter after the name.
  1695. */
  1696. unsigned int full_name_hash(const char *name, unsigned int len)
  1697. {
  1698. unsigned long a, x = 0, y = 0;
  1699. for (;;) {
  1700. if (!len)
  1701. goto done;
  1702. a = load_unaligned_zeropad(name);
  1703. if (len < sizeof(unsigned long))
  1704. break;
  1705. HASH_MIX(x, y, a);
  1706. name += sizeof(unsigned long);
  1707. len -= sizeof(unsigned long);
  1708. }
  1709. x ^= a & bytemask_from_count(len);
  1710. done:
  1711. return fold_hash(x, y);
  1712. }
  1713. EXPORT_SYMBOL(full_name_hash);
  1714. /* Return the "hash_len" (hash and length) of a null-terminated string */
  1715. u64 hashlen_string(const char *name)
  1716. {
  1717. unsigned long a = 0, x = 0, y = 0, adata, mask, len;
  1718. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1719. len = -sizeof(unsigned long);
  1720. do {
  1721. HASH_MIX(x, y, a);
  1722. len += sizeof(unsigned long);
  1723. a = load_unaligned_zeropad(name+len);
  1724. } while (!has_zero(a, &adata, &constants));
  1725. adata = prep_zero_mask(a, adata, &constants);
  1726. mask = create_zero_mask(adata);
  1727. x ^= a & zero_bytemask(mask);
  1728. return hashlen_create(fold_hash(x, y), len + find_zero(mask));
  1729. }
  1730. EXPORT_SYMBOL(hashlen_string);
  1731. /*
  1732. * Calculate the length and hash of the path component, and
  1733. * return the "hash_len" as the result.
  1734. */
  1735. static inline u64 hash_name(const char *name)
  1736. {
  1737. unsigned long a = 0, b, x = 0, y = 0, adata, bdata, mask, len;
  1738. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1739. len = -sizeof(unsigned long);
  1740. do {
  1741. HASH_MIX(x, y, a);
  1742. len += sizeof(unsigned long);
  1743. a = load_unaligned_zeropad(name+len);
  1744. b = a ^ REPEAT_BYTE('/');
  1745. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1746. adata = prep_zero_mask(a, adata, &constants);
  1747. bdata = prep_zero_mask(b, bdata, &constants);
  1748. mask = create_zero_mask(adata | bdata);
  1749. x ^= a & zero_bytemask(mask);
  1750. return hashlen_create(fold_hash(x, y), len + find_zero(mask));
  1751. }
  1752. #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
  1753. /* Return the hash of a string of known length */
  1754. unsigned int full_name_hash(const char *name, unsigned int len)
  1755. {
  1756. unsigned long hash = init_name_hash();
  1757. while (len--)
  1758. hash = partial_name_hash((unsigned char)*name++, hash);
  1759. return end_name_hash(hash);
  1760. }
  1761. EXPORT_SYMBOL(full_name_hash);
  1762. /* Return the "hash_len" (hash and length) of a null-terminated string */
  1763. u64 hashlen_string(const char *name)
  1764. {
  1765. unsigned long hash = init_name_hash();
  1766. unsigned long len = 0, c;
  1767. c = (unsigned char)*name;
  1768. while (c) {
  1769. len++;
  1770. hash = partial_name_hash(c, hash);
  1771. c = (unsigned char)name[len];
  1772. }
  1773. return hashlen_create(end_name_hash(hash), len);
  1774. }
  1775. EXPORT_SYMBOL(hashlen_string);
  1776. /*
  1777. * We know there's a real path component here of at least
  1778. * one character.
  1779. */
  1780. static inline u64 hash_name(const char *name)
  1781. {
  1782. unsigned long hash = init_name_hash();
  1783. unsigned long len = 0, c;
  1784. c = (unsigned char)*name;
  1785. do {
  1786. len++;
  1787. hash = partial_name_hash(c, hash);
  1788. c = (unsigned char)name[len];
  1789. } while (c && c != '/');
  1790. return hashlen_create(end_name_hash(hash), len);
  1791. }
  1792. #endif
  1793. /*
  1794. * Name resolution.
  1795. * This is the basic name resolution function, turning a pathname into
  1796. * the final dentry. We expect 'base' to be positive and a directory.
  1797. *
  1798. * Returns 0 and nd will have valid dentry and mnt on success.
  1799. * Returns error and drops reference to input namei data on failure.
  1800. */
  1801. static int link_path_walk(const char *name, struct nameidata *nd)
  1802. {
  1803. int err;
  1804. while (*name=='/')
  1805. name++;
  1806. if (!*name)
  1807. return 0;
  1808. /* At this point we know we have a real path component. */
  1809. for(;;) {
  1810. u64 hash_len;
  1811. int type;
  1812. err = may_lookup(nd);
  1813. if (err)
  1814. return err;
  1815. hash_len = hash_name(name);
  1816. type = LAST_NORM;
  1817. if (name[0] == '.') switch (hashlen_len(hash_len)) {
  1818. case 2:
  1819. if (name[1] == '.') {
  1820. type = LAST_DOTDOT;
  1821. nd->flags |= LOOKUP_JUMPED;
  1822. }
  1823. break;
  1824. case 1:
  1825. type = LAST_DOT;
  1826. }
  1827. if (likely(type == LAST_NORM)) {
  1828. struct dentry *parent = nd->path.dentry;
  1829. nd->flags &= ~LOOKUP_JUMPED;
  1830. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1831. struct qstr this = { { .hash_len = hash_len }, .name = name };
  1832. err = parent->d_op->d_hash(parent, &this);
  1833. if (err < 0)
  1834. return err;
  1835. hash_len = this.hash_len;
  1836. name = this.name;
  1837. }
  1838. }
  1839. nd->last.hash_len = hash_len;
  1840. nd->last.name = name;
  1841. nd->last_type = type;
  1842. name += hashlen_len(hash_len);
  1843. if (!*name)
  1844. goto OK;
  1845. /*
  1846. * If it wasn't NUL, we know it was '/'. Skip that
  1847. * slash, and continue until no more slashes.
  1848. */
  1849. do {
  1850. name++;
  1851. } while (unlikely(*name == '/'));
  1852. if (unlikely(!*name)) {
  1853. OK:
  1854. /* pathname body, done */
  1855. if (!nd->depth)
  1856. return 0;
  1857. name = nd->stack[nd->depth - 1].name;
  1858. /* trailing symlink, done */
  1859. if (!name)
  1860. return 0;
  1861. /* last component of nested symlink */
  1862. err = walk_component(nd, WALK_GET | WALK_PUT);
  1863. } else {
  1864. err = walk_component(nd, WALK_GET);
  1865. }
  1866. if (err < 0)
  1867. return err;
  1868. if (err) {
  1869. const char *s = get_link(nd);
  1870. if (IS_ERR(s))
  1871. return PTR_ERR(s);
  1872. err = 0;
  1873. if (unlikely(!s)) {
  1874. /* jumped */
  1875. put_link(nd);
  1876. } else {
  1877. nd->stack[nd->depth - 1].name = name;
  1878. name = s;
  1879. continue;
  1880. }
  1881. }
  1882. if (unlikely(!d_can_lookup(nd->path.dentry))) {
  1883. if (nd->flags & LOOKUP_RCU) {
  1884. if (unlazy_walk(nd, NULL, 0))
  1885. return -ECHILD;
  1886. }
  1887. return -ENOTDIR;
  1888. }
  1889. }
  1890. }
  1891. static const char *path_init(struct nameidata *nd, unsigned flags)
  1892. {
  1893. int retval = 0;
  1894. const char *s = nd->name->name;
  1895. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1896. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
  1897. nd->depth = 0;
  1898. if (flags & LOOKUP_ROOT) {
  1899. struct dentry *root = nd->root.dentry;
  1900. struct inode *inode = root->d_inode;
  1901. if (*s) {
  1902. if (!d_can_lookup(root))
  1903. return ERR_PTR(-ENOTDIR);
  1904. retval = inode_permission(inode, MAY_EXEC);
  1905. if (retval)
  1906. return ERR_PTR(retval);
  1907. }
  1908. nd->path = nd->root;
  1909. nd->inode = inode;
  1910. if (flags & LOOKUP_RCU) {
  1911. rcu_read_lock();
  1912. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1913. nd->root_seq = nd->seq;
  1914. nd->m_seq = read_seqbegin(&mount_lock);
  1915. } else {
  1916. path_get(&nd->path);
  1917. }
  1918. return s;
  1919. }
  1920. nd->root.mnt = NULL;
  1921. nd->path.mnt = NULL;
  1922. nd->path.dentry = NULL;
  1923. nd->m_seq = read_seqbegin(&mount_lock);
  1924. if (*s == '/') {
  1925. if (flags & LOOKUP_RCU)
  1926. rcu_read_lock();
  1927. set_root(nd);
  1928. if (likely(!nd_jump_root(nd)))
  1929. return s;
  1930. nd->root.mnt = NULL;
  1931. rcu_read_unlock();
  1932. return ERR_PTR(-ECHILD);
  1933. } else if (nd->dfd == AT_FDCWD) {
  1934. if (flags & LOOKUP_RCU) {
  1935. struct fs_struct *fs = current->fs;
  1936. unsigned seq;
  1937. rcu_read_lock();
  1938. do {
  1939. seq = read_seqcount_begin(&fs->seq);
  1940. nd->path = fs->pwd;
  1941. nd->inode = nd->path.dentry->d_inode;
  1942. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1943. } while (read_seqcount_retry(&fs->seq, seq));
  1944. } else {
  1945. get_fs_pwd(current->fs, &nd->path);
  1946. nd->inode = nd->path.dentry->d_inode;
  1947. }
  1948. return s;
  1949. } else {
  1950. /* Caller must check execute permissions on the starting path component */
  1951. struct fd f = fdget_raw(nd->dfd);
  1952. struct dentry *dentry;
  1953. if (!f.file)
  1954. return ERR_PTR(-EBADF);
  1955. dentry = f.file->f_path.dentry;
  1956. if (*s) {
  1957. if (!d_can_lookup(dentry)) {
  1958. fdput(f);
  1959. return ERR_PTR(-ENOTDIR);
  1960. }
  1961. }
  1962. nd->path = f.file->f_path;
  1963. if (flags & LOOKUP_RCU) {
  1964. rcu_read_lock();
  1965. nd->inode = nd->path.dentry->d_inode;
  1966. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1967. } else {
  1968. path_get(&nd->path);
  1969. nd->inode = nd->path.dentry->d_inode;
  1970. }
  1971. fdput(f);
  1972. return s;
  1973. }
  1974. }
  1975. static const char *trailing_symlink(struct nameidata *nd)
  1976. {
  1977. const char *s;
  1978. int error = may_follow_link(nd);
  1979. if (unlikely(error))
  1980. return ERR_PTR(error);
  1981. nd->flags |= LOOKUP_PARENT;
  1982. nd->stack[0].name = NULL;
  1983. s = get_link(nd);
  1984. return s ? s : "";
  1985. }
  1986. static inline int lookup_last(struct nameidata *nd)
  1987. {
  1988. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1989. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1990. nd->flags &= ~LOOKUP_PARENT;
  1991. return walk_component(nd,
  1992. nd->flags & LOOKUP_FOLLOW
  1993. ? nd->depth
  1994. ? WALK_PUT | WALK_GET
  1995. : WALK_GET
  1996. : 0);
  1997. }
  1998. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1999. static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
  2000. {
  2001. const char *s = path_init(nd, flags);
  2002. int err;
  2003. if (IS_ERR(s))
  2004. return PTR_ERR(s);
  2005. while (!(err = link_path_walk(s, nd))
  2006. && ((err = lookup_last(nd)) > 0)) {
  2007. s = trailing_symlink(nd);
  2008. if (IS_ERR(s)) {
  2009. err = PTR_ERR(s);
  2010. break;
  2011. }
  2012. }
  2013. if (!err)
  2014. err = complete_walk(nd);
  2015. if (!err && nd->flags & LOOKUP_DIRECTORY)
  2016. if (!d_can_lookup(nd->path.dentry))
  2017. err = -ENOTDIR;
  2018. if (!err) {
  2019. *path = nd->path;
  2020. nd->path.mnt = NULL;
  2021. nd->path.dentry = NULL;
  2022. }
  2023. terminate_walk(nd);
  2024. return err;
  2025. }
  2026. static int filename_lookup(int dfd, struct filename *name, unsigned flags,
  2027. struct path *path, struct path *root)
  2028. {
  2029. int retval;
  2030. struct nameidata nd;
  2031. if (IS_ERR(name))
  2032. return PTR_ERR(name);
  2033. if (unlikely(root)) {
  2034. nd.root = *root;
  2035. flags |= LOOKUP_ROOT;
  2036. }
  2037. set_nameidata(&nd, dfd, name);
  2038. retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
  2039. if (unlikely(retval == -ECHILD))
  2040. retval = path_lookupat(&nd, flags, path);
  2041. if (unlikely(retval == -ESTALE))
  2042. retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
  2043. if (likely(!retval))
  2044. audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
  2045. restore_nameidata();
  2046. putname(name);
  2047. return retval;
  2048. }
  2049. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  2050. static int path_parentat(struct nameidata *nd, unsigned flags,
  2051. struct path *parent)
  2052. {
  2053. const char *s = path_init(nd, flags);
  2054. int err;
  2055. if (IS_ERR(s))
  2056. return PTR_ERR(s);
  2057. err = link_path_walk(s, nd);
  2058. if (!err)
  2059. err = complete_walk(nd);
  2060. if (!err) {
  2061. *parent = nd->path;
  2062. nd->path.mnt = NULL;
  2063. nd->path.dentry = NULL;
  2064. }
  2065. terminate_walk(nd);
  2066. return err;
  2067. }
  2068. static struct filename *filename_parentat(int dfd, struct filename *name,
  2069. unsigned int flags, struct path *parent,
  2070. struct qstr *last, int *type)
  2071. {
  2072. int retval;
  2073. struct nameidata nd;
  2074. if (IS_ERR(name))
  2075. return name;
  2076. set_nameidata(&nd, dfd, name);
  2077. retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
  2078. if (unlikely(retval == -ECHILD))
  2079. retval = path_parentat(&nd, flags, parent);
  2080. if (unlikely(retval == -ESTALE))
  2081. retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
  2082. if (likely(!retval)) {
  2083. *last = nd.last;
  2084. *type = nd.last_type;
  2085. audit_inode(name, parent->dentry, LOOKUP_PARENT);
  2086. } else {
  2087. putname(name);
  2088. name = ERR_PTR(retval);
  2089. }
  2090. restore_nameidata();
  2091. return name;
  2092. }
  2093. /* does lookup, returns the object with parent locked */
  2094. struct dentry *kern_path_locked(const char *name, struct path *path)
  2095. {
  2096. struct filename *filename;
  2097. struct dentry *d;
  2098. struct qstr last;
  2099. int type;
  2100. filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
  2101. &last, &type);
  2102. if (IS_ERR(filename))
  2103. return ERR_CAST(filename);
  2104. if (unlikely(type != LAST_NORM)) {
  2105. path_put(path);
  2106. putname(filename);
  2107. return ERR_PTR(-EINVAL);
  2108. }
  2109. inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
  2110. d = __lookup_hash(&last, path->dentry, 0);
  2111. if (IS_ERR(d)) {
  2112. inode_unlock(path->dentry->d_inode);
  2113. path_put(path);
  2114. }
  2115. putname(filename);
  2116. return d;
  2117. }
  2118. int kern_path(const char *name, unsigned int flags, struct path *path)
  2119. {
  2120. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2121. flags, path, NULL);
  2122. }
  2123. EXPORT_SYMBOL(kern_path);
  2124. /**
  2125. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  2126. * @dentry: pointer to dentry of the base directory
  2127. * @mnt: pointer to vfs mount of the base directory
  2128. * @name: pointer to file name
  2129. * @flags: lookup flags
  2130. * @path: pointer to struct path to fill
  2131. */
  2132. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  2133. const char *name, unsigned int flags,
  2134. struct path *path)
  2135. {
  2136. struct path root = {.mnt = mnt, .dentry = dentry};
  2137. /* the first argument of filename_lookup() is ignored with root */
  2138. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2139. flags , path, &root);
  2140. }
  2141. EXPORT_SYMBOL(vfs_path_lookup);
  2142. /**
  2143. * lookup_hash - lookup single pathname component on already hashed name
  2144. * @name: name and hash to lookup
  2145. * @base: base directory to lookup from
  2146. *
  2147. * The name must have been verified and hashed (see lookup_one_len()). Using
  2148. * this after just full_name_hash() is unsafe.
  2149. *
  2150. * This function also doesn't check for search permission on base directory.
  2151. *
  2152. * Use lookup_one_len_unlocked() instead, unless you really know what you are
  2153. * doing.
  2154. *
  2155. * Do not hold i_mutex; this helper takes i_mutex if necessary.
  2156. */
  2157. struct dentry *lookup_hash(const struct qstr *name, struct dentry *base)
  2158. {
  2159. struct dentry *ret;
  2160. ret = lookup_dcache(name, base, 0);
  2161. if (!ret)
  2162. ret = lookup_slow(name, base, 0);
  2163. return ret;
  2164. }
  2165. EXPORT_SYMBOL(lookup_hash);
  2166. /**
  2167. * lookup_one_len - filesystem helper to lookup single pathname component
  2168. * @name: pathname component to lookup
  2169. * @base: base directory to lookup from
  2170. * @len: maximum length @len should be interpreted to
  2171. *
  2172. * Note that this routine is purely a helper for filesystem usage and should
  2173. * not be called by generic code.
  2174. *
  2175. * The caller must hold base->i_mutex.
  2176. */
  2177. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  2178. {
  2179. struct qstr this;
  2180. unsigned int c;
  2181. int err;
  2182. WARN_ON_ONCE(!inode_is_locked(base->d_inode));
  2183. this.name = name;
  2184. this.len = len;
  2185. this.hash = full_name_hash(name, len);
  2186. if (!len)
  2187. return ERR_PTR(-EACCES);
  2188. if (unlikely(name[0] == '.')) {
  2189. if (len < 2 || (len == 2 && name[1] == '.'))
  2190. return ERR_PTR(-EACCES);
  2191. }
  2192. while (len--) {
  2193. c = *(const unsigned char *)name++;
  2194. if (c == '/' || c == '\0')
  2195. return ERR_PTR(-EACCES);
  2196. }
  2197. /*
  2198. * See if the low-level filesystem might want
  2199. * to use its own hash..
  2200. */
  2201. if (base->d_flags & DCACHE_OP_HASH) {
  2202. int err = base->d_op->d_hash(base, &this);
  2203. if (err < 0)
  2204. return ERR_PTR(err);
  2205. }
  2206. err = inode_permission(base->d_inode, MAY_EXEC);
  2207. if (err)
  2208. return ERR_PTR(err);
  2209. return __lookup_hash(&this, base, 0);
  2210. }
  2211. EXPORT_SYMBOL(lookup_one_len);
  2212. /**
  2213. * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
  2214. * @name: pathname component to lookup
  2215. * @base: base directory to lookup from
  2216. * @len: maximum length @len should be interpreted to
  2217. *
  2218. * Note that this routine is purely a helper for filesystem usage and should
  2219. * not be called by generic code.
  2220. *
  2221. * Unlike lookup_one_len, it should be called without the parent
  2222. * i_mutex held, and will take the i_mutex itself if necessary.
  2223. */
  2224. struct dentry *lookup_one_len_unlocked(const char *name,
  2225. struct dentry *base, int len)
  2226. {
  2227. struct qstr this;
  2228. unsigned int c;
  2229. int err;
  2230. this.name = name;
  2231. this.len = len;
  2232. this.hash = full_name_hash(name, len);
  2233. if (!len)
  2234. return ERR_PTR(-EACCES);
  2235. if (unlikely(name[0] == '.')) {
  2236. if (len < 2 || (len == 2 && name[1] == '.'))
  2237. return ERR_PTR(-EACCES);
  2238. }
  2239. while (len--) {
  2240. c = *(const unsigned char *)name++;
  2241. if (c == '/' || c == '\0')
  2242. return ERR_PTR(-EACCES);
  2243. }
  2244. /*
  2245. * See if the low-level filesystem might want
  2246. * to use its own hash..
  2247. */
  2248. if (base->d_flags & DCACHE_OP_HASH) {
  2249. int err = base->d_op->d_hash(base, &this);
  2250. if (err < 0)
  2251. return ERR_PTR(err);
  2252. }
  2253. err = inode_permission(base->d_inode, MAY_EXEC);
  2254. if (err)
  2255. return ERR_PTR(err);
  2256. return lookup_hash(&this, base);
  2257. }
  2258. EXPORT_SYMBOL(lookup_one_len_unlocked);
  2259. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  2260. struct path *path, int *empty)
  2261. {
  2262. return filename_lookup(dfd, getname_flags(name, flags, empty),
  2263. flags, path, NULL);
  2264. }
  2265. EXPORT_SYMBOL(user_path_at_empty);
  2266. /*
  2267. * NB: most callers don't do anything directly with the reference to the
  2268. * to struct filename, but the nd->last pointer points into the name string
  2269. * allocated by getname. So we must hold the reference to it until all
  2270. * path-walking is complete.
  2271. */
  2272. static inline struct filename *
  2273. user_path_parent(int dfd, const char __user *path,
  2274. struct path *parent,
  2275. struct qstr *last,
  2276. int *type,
  2277. unsigned int flags)
  2278. {
  2279. /* only LOOKUP_REVAL is allowed in extra flags */
  2280. return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
  2281. parent, last, type);
  2282. }
  2283. /**
  2284. * mountpoint_last - look up last component for umount
  2285. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  2286. * @path: pointer to container for result
  2287. *
  2288. * This is a special lookup_last function just for umount. In this case, we
  2289. * need to resolve the path without doing any revalidation.
  2290. *
  2291. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  2292. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  2293. * in almost all cases, this lookup will be served out of the dcache. The only
  2294. * cases where it won't are if nd->last refers to a symlink or the path is
  2295. * bogus and it doesn't exist.
  2296. *
  2297. * Returns:
  2298. * -error: if there was an error during lookup. This includes -ENOENT if the
  2299. * lookup found a negative dentry. The nd->path reference will also be
  2300. * put in this case.
  2301. *
  2302. * 0: if we successfully resolved nd->path and found it to not to be a
  2303. * symlink that needs to be followed. "path" will also be populated.
  2304. * The nd->path reference will also be put.
  2305. *
  2306. * 1: if we successfully resolved nd->last and found it to be a symlink
  2307. * that needs to be followed. "path" will be populated with the path
  2308. * to the link, and nd->path will *not* be put.
  2309. */
  2310. static int
  2311. mountpoint_last(struct nameidata *nd, struct path *path)
  2312. {
  2313. int error = 0;
  2314. struct dentry *dentry;
  2315. struct dentry *dir = nd->path.dentry;
  2316. /* If we're in rcuwalk, drop out of it to handle last component */
  2317. if (nd->flags & LOOKUP_RCU) {
  2318. if (unlazy_walk(nd, NULL, 0))
  2319. return -ECHILD;
  2320. }
  2321. nd->flags &= ~LOOKUP_PARENT;
  2322. if (unlikely(nd->last_type != LAST_NORM)) {
  2323. error = handle_dots(nd, nd->last_type);
  2324. if (error)
  2325. return error;
  2326. dentry = dget(nd->path.dentry);
  2327. } else {
  2328. dentry = d_lookup(dir, &nd->last);
  2329. if (!dentry) {
  2330. /*
  2331. * No cached dentry. Mounted dentries are pinned in the
  2332. * cache, so that means that this dentry is probably
  2333. * a symlink or the path doesn't actually point
  2334. * to a mounted dentry.
  2335. */
  2336. dentry = lookup_slow(&nd->last, dir,
  2337. nd->flags | LOOKUP_NO_REVAL);
  2338. if (IS_ERR(dentry))
  2339. return PTR_ERR(dentry);
  2340. }
  2341. }
  2342. if (d_is_negative(dentry)) {
  2343. dput(dentry);
  2344. return -ENOENT;
  2345. }
  2346. if (nd->depth)
  2347. put_link(nd);
  2348. path->dentry = dentry;
  2349. path->mnt = nd->path.mnt;
  2350. error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
  2351. d_backing_inode(dentry), 0);
  2352. if (unlikely(error))
  2353. return error;
  2354. mntget(path->mnt);
  2355. follow_mount(path);
  2356. return 0;
  2357. }
  2358. /**
  2359. * path_mountpoint - look up a path to be umounted
  2360. * @nd: lookup context
  2361. * @flags: lookup flags
  2362. * @path: pointer to container for result
  2363. *
  2364. * Look up the given name, but don't attempt to revalidate the last component.
  2365. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  2366. */
  2367. static int
  2368. path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
  2369. {
  2370. const char *s = path_init(nd, flags);
  2371. int err;
  2372. if (IS_ERR(s))
  2373. return PTR_ERR(s);
  2374. while (!(err = link_path_walk(s, nd)) &&
  2375. (err = mountpoint_last(nd, path)) > 0) {
  2376. s = trailing_symlink(nd);
  2377. if (IS_ERR(s)) {
  2378. err = PTR_ERR(s);
  2379. break;
  2380. }
  2381. }
  2382. terminate_walk(nd);
  2383. return err;
  2384. }
  2385. static int
  2386. filename_mountpoint(int dfd, struct filename *name, struct path *path,
  2387. unsigned int flags)
  2388. {
  2389. struct nameidata nd;
  2390. int error;
  2391. if (IS_ERR(name))
  2392. return PTR_ERR(name);
  2393. set_nameidata(&nd, dfd, name);
  2394. error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
  2395. if (unlikely(error == -ECHILD))
  2396. error = path_mountpoint(&nd, flags, path);
  2397. if (unlikely(error == -ESTALE))
  2398. error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
  2399. if (likely(!error))
  2400. audit_inode(name, path->dentry, 0);
  2401. restore_nameidata();
  2402. putname(name);
  2403. return error;
  2404. }
  2405. /**
  2406. * user_path_mountpoint_at - lookup a path from userland in order to umount it
  2407. * @dfd: directory file descriptor
  2408. * @name: pathname from userland
  2409. * @flags: lookup flags
  2410. * @path: pointer to container to hold result
  2411. *
  2412. * A umount is a special case for path walking. We're not actually interested
  2413. * in the inode in this situation, and ESTALE errors can be a problem. We
  2414. * simply want track down the dentry and vfsmount attached at the mountpoint
  2415. * and avoid revalidating the last component.
  2416. *
  2417. * Returns 0 and populates "path" on success.
  2418. */
  2419. int
  2420. user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
  2421. struct path *path)
  2422. {
  2423. return filename_mountpoint(dfd, getname(name), path, flags);
  2424. }
  2425. int
  2426. kern_path_mountpoint(int dfd, const char *name, struct path *path,
  2427. unsigned int flags)
  2428. {
  2429. return filename_mountpoint(dfd, getname_kernel(name), path, flags);
  2430. }
  2431. EXPORT_SYMBOL(kern_path_mountpoint);
  2432. int __check_sticky(struct inode *dir, struct inode *inode)
  2433. {
  2434. kuid_t fsuid = current_fsuid();
  2435. if (uid_eq(inode->i_uid, fsuid))
  2436. return 0;
  2437. if (uid_eq(dir->i_uid, fsuid))
  2438. return 0;
  2439. return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
  2440. }
  2441. EXPORT_SYMBOL(__check_sticky);
  2442. /*
  2443. * Check whether we can remove a link victim from directory dir, check
  2444. * whether the type of victim is right.
  2445. * 1. We can't do it if dir is read-only (done in permission())
  2446. * 2. We should have write and exec permissions on dir
  2447. * 3. We can't remove anything from append-only dir
  2448. * 4. We can't do anything with immutable dir (done in permission())
  2449. * 5. If the sticky bit on dir is set we should either
  2450. * a. be owner of dir, or
  2451. * b. be owner of victim, or
  2452. * c. have CAP_FOWNER capability
  2453. * 6. If the victim is append-only or immutable we can't do antyhing with
  2454. * links pointing to it.
  2455. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  2456. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  2457. * 9. We can't remove a root or mountpoint.
  2458. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  2459. * nfs_async_unlink().
  2460. */
  2461. static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
  2462. {
  2463. struct inode *inode = d_backing_inode(victim);
  2464. int error;
  2465. if (d_is_negative(victim))
  2466. return -ENOENT;
  2467. BUG_ON(!inode);
  2468. BUG_ON(victim->d_parent->d_inode != dir);
  2469. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  2470. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2471. if (error)
  2472. return error;
  2473. if (IS_APPEND(dir))
  2474. return -EPERM;
  2475. if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  2476. IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
  2477. return -EPERM;
  2478. if (isdir) {
  2479. if (!d_is_dir(victim))
  2480. return -ENOTDIR;
  2481. if (IS_ROOT(victim))
  2482. return -EBUSY;
  2483. } else if (d_is_dir(victim))
  2484. return -EISDIR;
  2485. if (IS_DEADDIR(dir))
  2486. return -ENOENT;
  2487. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  2488. return -EBUSY;
  2489. return 0;
  2490. }
  2491. /* Check whether we can create an object with dentry child in directory
  2492. * dir.
  2493. * 1. We can't do it if child already exists (open has special treatment for
  2494. * this case, but since we are inlined it's OK)
  2495. * 2. We can't do it if dir is read-only (done in permission())
  2496. * 3. We should have write and exec permissions on dir
  2497. * 4. We can't do it if dir is immutable (done in permission())
  2498. */
  2499. static inline int may_create(struct inode *dir, struct dentry *child)
  2500. {
  2501. audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
  2502. if (child->d_inode)
  2503. return -EEXIST;
  2504. if (IS_DEADDIR(dir))
  2505. return -ENOENT;
  2506. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2507. }
  2508. /*
  2509. * p1 and p2 should be directories on the same fs.
  2510. */
  2511. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  2512. {
  2513. struct dentry *p;
  2514. if (p1 == p2) {
  2515. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2516. return NULL;
  2517. }
  2518. mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
  2519. p = d_ancestor(p2, p1);
  2520. if (p) {
  2521. inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
  2522. inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
  2523. return p;
  2524. }
  2525. p = d_ancestor(p1, p2);
  2526. if (p) {
  2527. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2528. inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
  2529. return p;
  2530. }
  2531. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2532. inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
  2533. return NULL;
  2534. }
  2535. EXPORT_SYMBOL(lock_rename);
  2536. void unlock_rename(struct dentry *p1, struct dentry *p2)
  2537. {
  2538. inode_unlock(p1->d_inode);
  2539. if (p1 != p2) {
  2540. inode_unlock(p2->d_inode);
  2541. mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
  2542. }
  2543. }
  2544. EXPORT_SYMBOL(unlock_rename);
  2545. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2546. bool want_excl)
  2547. {
  2548. int error = may_create(dir, dentry);
  2549. if (error)
  2550. return error;
  2551. if (!dir->i_op->create)
  2552. return -EACCES; /* shouldn't it be ENOSYS? */
  2553. mode &= S_IALLUGO;
  2554. mode |= S_IFREG;
  2555. error = security_inode_create(dir, dentry, mode);
  2556. if (error)
  2557. return error;
  2558. error = dir->i_op->create(dir, dentry, mode, want_excl);
  2559. if (!error)
  2560. fsnotify_create(dir, dentry);
  2561. return error;
  2562. }
  2563. EXPORT_SYMBOL(vfs_create);
  2564. static int may_open(struct path *path, int acc_mode, int flag)
  2565. {
  2566. struct dentry *dentry = path->dentry;
  2567. struct inode *inode = dentry->d_inode;
  2568. int error;
  2569. if (!inode)
  2570. return -ENOENT;
  2571. switch (inode->i_mode & S_IFMT) {
  2572. case S_IFLNK:
  2573. return -ELOOP;
  2574. case S_IFDIR:
  2575. if (acc_mode & MAY_WRITE)
  2576. return -EISDIR;
  2577. break;
  2578. case S_IFBLK:
  2579. case S_IFCHR:
  2580. if (path->mnt->mnt_flags & MNT_NODEV)
  2581. return -EACCES;
  2582. /*FALLTHRU*/
  2583. case S_IFIFO:
  2584. case S_IFSOCK:
  2585. flag &= ~O_TRUNC;
  2586. break;
  2587. }
  2588. error = inode_permission(inode, MAY_OPEN | acc_mode);
  2589. if (error)
  2590. return error;
  2591. /*
  2592. * An append-only file must be opened in append mode for writing.
  2593. */
  2594. if (IS_APPEND(inode)) {
  2595. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  2596. return -EPERM;
  2597. if (flag & O_TRUNC)
  2598. return -EPERM;
  2599. }
  2600. /* O_NOATIME can only be set by the owner or superuser */
  2601. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  2602. return -EPERM;
  2603. return 0;
  2604. }
  2605. static int handle_truncate(struct file *filp)
  2606. {
  2607. struct path *path = &filp->f_path;
  2608. struct inode *inode = path->dentry->d_inode;
  2609. int error = get_write_access(inode);
  2610. if (error)
  2611. return error;
  2612. /*
  2613. * Refuse to truncate files with mandatory locks held on them.
  2614. */
  2615. error = locks_verify_locked(filp);
  2616. if (!error)
  2617. error = security_path_truncate(path);
  2618. if (!error) {
  2619. error = do_truncate(path->dentry, 0,
  2620. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  2621. filp);
  2622. }
  2623. put_write_access(inode);
  2624. return error;
  2625. }
  2626. static inline int open_to_namei_flags(int flag)
  2627. {
  2628. if ((flag & O_ACCMODE) == 3)
  2629. flag--;
  2630. return flag;
  2631. }
  2632. static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
  2633. {
  2634. int error = security_path_mknod(dir, dentry, mode, 0);
  2635. if (error)
  2636. return error;
  2637. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  2638. if (error)
  2639. return error;
  2640. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  2641. }
  2642. /*
  2643. * Attempt to atomically look up, create and open a file from a negative
  2644. * dentry.
  2645. *
  2646. * Returns 0 if successful. The file will have been created and attached to
  2647. * @file by the filesystem calling finish_open().
  2648. *
  2649. * Returns 1 if the file was looked up only or didn't need creating. The
  2650. * caller will need to perform the open themselves. @path will have been
  2651. * updated to point to the new dentry. This may be negative.
  2652. *
  2653. * Returns an error code otherwise.
  2654. */
  2655. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2656. struct path *path, struct file *file,
  2657. const struct open_flags *op,
  2658. int open_flag, umode_t mode,
  2659. int *opened)
  2660. {
  2661. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2662. struct inode *dir = nd->path.dentry->d_inode;
  2663. int error;
  2664. if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
  2665. open_flag &= ~O_TRUNC;
  2666. if (nd->flags & LOOKUP_DIRECTORY)
  2667. open_flag |= O_DIRECTORY;
  2668. file->f_path.dentry = DENTRY_NOT_SET;
  2669. file->f_path.mnt = nd->path.mnt;
  2670. error = dir->i_op->atomic_open(dir, dentry, file,
  2671. open_to_namei_flags(open_flag),
  2672. mode, opened);
  2673. d_lookup_done(dentry);
  2674. if (!error) {
  2675. /*
  2676. * We didn't have the inode before the open, so check open
  2677. * permission here.
  2678. */
  2679. int acc_mode = op->acc_mode;
  2680. if (*opened & FILE_CREATED) {
  2681. WARN_ON(!(open_flag & O_CREAT));
  2682. fsnotify_create(dir, dentry);
  2683. acc_mode = 0;
  2684. }
  2685. error = may_open(&file->f_path, acc_mode, open_flag);
  2686. if (WARN_ON(error > 0))
  2687. error = -EINVAL;
  2688. } else if (error > 0) {
  2689. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2690. error = -EIO;
  2691. } else {
  2692. if (file->f_path.dentry) {
  2693. dput(dentry);
  2694. dentry = file->f_path.dentry;
  2695. }
  2696. if (*opened & FILE_CREATED)
  2697. fsnotify_create(dir, dentry);
  2698. if (unlikely(d_is_negative(dentry))) {
  2699. error = -ENOENT;
  2700. } else {
  2701. path->dentry = dentry;
  2702. path->mnt = nd->path.mnt;
  2703. return 1;
  2704. }
  2705. }
  2706. }
  2707. dput(dentry);
  2708. return error;
  2709. }
  2710. /*
  2711. * Look up and maybe create and open the last component.
  2712. *
  2713. * Must be called with i_mutex held on parent.
  2714. *
  2715. * Returns 0 if the file was successfully atomically created (if necessary) and
  2716. * opened. In this case the file will be returned attached to @file.
  2717. *
  2718. * Returns 1 if the file was not completely opened at this time, though lookups
  2719. * and creations will have been performed and the dentry returned in @path will
  2720. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2721. * specified then a negative dentry may be returned.
  2722. *
  2723. * An error code is returned otherwise.
  2724. *
  2725. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2726. * cleared otherwise prior to returning.
  2727. */
  2728. static int lookup_open(struct nameidata *nd, struct path *path,
  2729. struct file *file,
  2730. const struct open_flags *op,
  2731. bool got_write, int *opened)
  2732. {
  2733. struct dentry *dir = nd->path.dentry;
  2734. struct inode *dir_inode = dir->d_inode;
  2735. int open_flag = op->open_flag;
  2736. struct dentry *dentry;
  2737. int error, create_error = 0;
  2738. umode_t mode = op->mode;
  2739. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  2740. if (unlikely(IS_DEADDIR(dir_inode)))
  2741. return -ENOENT;
  2742. *opened &= ~FILE_CREATED;
  2743. dentry = d_lookup(dir, &nd->last);
  2744. for (;;) {
  2745. if (!dentry) {
  2746. dentry = d_alloc_parallel(dir, &nd->last, &wq);
  2747. if (IS_ERR(dentry))
  2748. return PTR_ERR(dentry);
  2749. }
  2750. if (d_in_lookup(dentry))
  2751. break;
  2752. if (!(dentry->d_flags & DCACHE_OP_REVALIDATE))
  2753. break;
  2754. error = d_revalidate(dentry, nd->flags);
  2755. if (likely(error > 0))
  2756. break;
  2757. if (error)
  2758. goto out_dput;
  2759. d_invalidate(dentry);
  2760. dput(dentry);
  2761. dentry = NULL;
  2762. }
  2763. if (dentry->d_inode) {
  2764. /* Cached positive dentry: will open in f_op->open */
  2765. goto out_no_open;
  2766. }
  2767. /*
  2768. * Checking write permission is tricky, bacuse we don't know if we are
  2769. * going to actually need it: O_CREAT opens should work as long as the
  2770. * file exists. But checking existence breaks atomicity. The trick is
  2771. * to check access and if not granted clear O_CREAT from the flags.
  2772. *
  2773. * Another problem is returing the "right" error value (e.g. for an
  2774. * O_EXCL open we want to return EEXIST not EROFS).
  2775. */
  2776. if (open_flag & O_CREAT) {
  2777. if (!IS_POSIXACL(dir->d_inode))
  2778. mode &= ~current_umask();
  2779. if (unlikely(!got_write)) {
  2780. create_error = -EROFS;
  2781. open_flag &= ~O_CREAT;
  2782. if (open_flag & (O_EXCL | O_TRUNC))
  2783. goto no_open;
  2784. /* No side effects, safe to clear O_CREAT */
  2785. } else {
  2786. create_error = may_o_create(&nd->path, dentry, mode);
  2787. if (create_error) {
  2788. open_flag &= ~O_CREAT;
  2789. if (open_flag & O_EXCL)
  2790. goto no_open;
  2791. }
  2792. }
  2793. } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
  2794. unlikely(!got_write)) {
  2795. /*
  2796. * No O_CREATE -> atomicity not a requirement -> fall
  2797. * back to lookup + open
  2798. */
  2799. goto no_open;
  2800. }
  2801. if (dir_inode->i_op->atomic_open) {
  2802. error = atomic_open(nd, dentry, path, file, op, open_flag,
  2803. mode, opened);
  2804. if (unlikely(error == -ENOENT) && create_error)
  2805. error = create_error;
  2806. return error;
  2807. }
  2808. no_open:
  2809. if (d_in_lookup(dentry)) {
  2810. struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
  2811. nd->flags);
  2812. d_lookup_done(dentry);
  2813. if (unlikely(res)) {
  2814. if (IS_ERR(res)) {
  2815. error = PTR_ERR(res);
  2816. goto out_dput;
  2817. }
  2818. dput(dentry);
  2819. dentry = res;
  2820. }
  2821. }
  2822. /* Negative dentry, just create the file */
  2823. if (!dentry->d_inode && (open_flag & O_CREAT)) {
  2824. *opened |= FILE_CREATED;
  2825. audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  2826. if (!dir_inode->i_op->create) {
  2827. error = -EACCES;
  2828. goto out_dput;
  2829. }
  2830. error = dir_inode->i_op->create(dir_inode, dentry, mode,
  2831. open_flag & O_EXCL);
  2832. if (error)
  2833. goto out_dput;
  2834. fsnotify_create(dir_inode, dentry);
  2835. }
  2836. if (unlikely(create_error) && !dentry->d_inode) {
  2837. error = create_error;
  2838. goto out_dput;
  2839. }
  2840. out_no_open:
  2841. path->dentry = dentry;
  2842. path->mnt = nd->path.mnt;
  2843. return 1;
  2844. out_dput:
  2845. dput(dentry);
  2846. return error;
  2847. }
  2848. /*
  2849. * Handle the last step of open()
  2850. */
  2851. static int do_last(struct nameidata *nd,
  2852. struct file *file, const struct open_flags *op,
  2853. int *opened)
  2854. {
  2855. struct dentry *dir = nd->path.dentry;
  2856. int open_flag = op->open_flag;
  2857. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2858. bool got_write = false;
  2859. int acc_mode = op->acc_mode;
  2860. unsigned seq;
  2861. struct inode *inode;
  2862. struct path path;
  2863. int error;
  2864. nd->flags &= ~LOOKUP_PARENT;
  2865. nd->flags |= op->intent;
  2866. if (nd->last_type != LAST_NORM) {
  2867. error = handle_dots(nd, nd->last_type);
  2868. if (unlikely(error))
  2869. return error;
  2870. goto finish_open;
  2871. }
  2872. if (!(open_flag & O_CREAT)) {
  2873. if (nd->last.name[nd->last.len])
  2874. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2875. /* we _can_ be in RCU mode here */
  2876. error = lookup_fast(nd, &path, &inode, &seq);
  2877. if (likely(error > 0))
  2878. goto finish_lookup;
  2879. if (error < 0)
  2880. return error;
  2881. BUG_ON(nd->inode != dir->d_inode);
  2882. BUG_ON(nd->flags & LOOKUP_RCU);
  2883. } else {
  2884. /* create side of things */
  2885. /*
  2886. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2887. * has been cleared when we got to the last component we are
  2888. * about to look up
  2889. */
  2890. error = complete_walk(nd);
  2891. if (error)
  2892. return error;
  2893. audit_inode(nd->name, dir, LOOKUP_PARENT);
  2894. /* trailing slashes? */
  2895. if (unlikely(nd->last.name[nd->last.len]))
  2896. return -EISDIR;
  2897. }
  2898. if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  2899. error = mnt_want_write(nd->path.mnt);
  2900. if (!error)
  2901. got_write = true;
  2902. /*
  2903. * do _not_ fail yet - we might not need that or fail with
  2904. * a different error; let lookup_open() decide; we'll be
  2905. * dropping this one anyway.
  2906. */
  2907. }
  2908. if (open_flag & O_CREAT)
  2909. inode_lock(dir->d_inode);
  2910. else
  2911. inode_lock_shared(dir->d_inode);
  2912. error = lookup_open(nd, &path, file, op, got_write, opened);
  2913. if (open_flag & O_CREAT)
  2914. inode_unlock(dir->d_inode);
  2915. else
  2916. inode_unlock_shared(dir->d_inode);
  2917. if (error <= 0) {
  2918. if (error)
  2919. goto out;
  2920. if ((*opened & FILE_CREATED) ||
  2921. !S_ISREG(file_inode(file)->i_mode))
  2922. will_truncate = false;
  2923. audit_inode(nd->name, file->f_path.dentry, 0);
  2924. goto opened;
  2925. }
  2926. if (*opened & FILE_CREATED) {
  2927. /* Don't check for write permission, don't truncate */
  2928. open_flag &= ~O_TRUNC;
  2929. will_truncate = false;
  2930. acc_mode = 0;
  2931. path_to_nameidata(&path, nd);
  2932. goto finish_open_created;
  2933. }
  2934. /*
  2935. * If atomic_open() acquired write access it is dropped now due to
  2936. * possible mount and symlink following (this might be optimized away if
  2937. * necessary...)
  2938. */
  2939. if (got_write) {
  2940. mnt_drop_write(nd->path.mnt);
  2941. got_write = false;
  2942. }
  2943. error = follow_managed(&path, nd);
  2944. if (unlikely(error < 0))
  2945. return error;
  2946. if (unlikely(d_is_negative(path.dentry))) {
  2947. path_to_nameidata(&path, nd);
  2948. return -ENOENT;
  2949. }
  2950. /*
  2951. * create/update audit record if it already exists.
  2952. */
  2953. audit_inode(nd->name, path.dentry, 0);
  2954. if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
  2955. path_to_nameidata(&path, nd);
  2956. return -EEXIST;
  2957. }
  2958. seq = 0; /* out of RCU mode, so the value doesn't matter */
  2959. inode = d_backing_inode(path.dentry);
  2960. finish_lookup:
  2961. if (nd->depth)
  2962. put_link(nd);
  2963. error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
  2964. inode, seq);
  2965. if (unlikely(error))
  2966. return error;
  2967. path_to_nameidata(&path, nd);
  2968. nd->inode = inode;
  2969. nd->seq = seq;
  2970. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2971. finish_open:
  2972. error = complete_walk(nd);
  2973. if (error)
  2974. return error;
  2975. audit_inode(nd->name, nd->path.dentry, 0);
  2976. error = -EISDIR;
  2977. if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
  2978. goto out;
  2979. error = -ENOTDIR;
  2980. if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
  2981. goto out;
  2982. if (!d_is_reg(nd->path.dentry))
  2983. will_truncate = false;
  2984. if (will_truncate) {
  2985. error = mnt_want_write(nd->path.mnt);
  2986. if (error)
  2987. goto out;
  2988. got_write = true;
  2989. }
  2990. finish_open_created:
  2991. error = may_open(&nd->path, acc_mode, open_flag);
  2992. if (error)
  2993. goto out;
  2994. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  2995. error = vfs_open(&nd->path, file, current_cred());
  2996. if (error)
  2997. goto out;
  2998. *opened |= FILE_OPENED;
  2999. opened:
  3000. error = open_check_o_direct(file);
  3001. if (!error)
  3002. error = ima_file_check(file, op->acc_mode, *opened);
  3003. if (!error && will_truncate)
  3004. error = handle_truncate(file);
  3005. out:
  3006. if (unlikely(error) && (*opened & FILE_OPENED))
  3007. fput(file);
  3008. if (unlikely(error > 0)) {
  3009. WARN_ON(1);
  3010. error = -EINVAL;
  3011. }
  3012. if (got_write)
  3013. mnt_drop_write(nd->path.mnt);
  3014. return error;
  3015. }
  3016. static int do_tmpfile(struct nameidata *nd, unsigned flags,
  3017. const struct open_flags *op,
  3018. struct file *file, int *opened)
  3019. {
  3020. static const struct qstr name = QSTR_INIT("/", 1);
  3021. struct dentry *child;
  3022. struct inode *dir;
  3023. struct path path;
  3024. int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
  3025. if (unlikely(error))
  3026. return error;
  3027. error = mnt_want_write(path.mnt);
  3028. if (unlikely(error))
  3029. goto out;
  3030. dir = path.dentry->d_inode;
  3031. /* we want directory to be writable */
  3032. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  3033. if (error)
  3034. goto out2;
  3035. if (!dir->i_op->tmpfile) {
  3036. error = -EOPNOTSUPP;
  3037. goto out2;
  3038. }
  3039. child = d_alloc(path.dentry, &name);
  3040. if (unlikely(!child)) {
  3041. error = -ENOMEM;
  3042. goto out2;
  3043. }
  3044. dput(path.dentry);
  3045. path.dentry = child;
  3046. error = dir->i_op->tmpfile(dir, child, op->mode);
  3047. if (error)
  3048. goto out2;
  3049. audit_inode(nd->name, child, 0);
  3050. /* Don't check for other permissions, the inode was just created */
  3051. error = may_open(&path, 0, op->open_flag);
  3052. if (error)
  3053. goto out2;
  3054. file->f_path.mnt = path.mnt;
  3055. error = finish_open(file, child, NULL, opened);
  3056. if (error)
  3057. goto out2;
  3058. error = open_check_o_direct(file);
  3059. if (error) {
  3060. fput(file);
  3061. } else if (!(op->open_flag & O_EXCL)) {
  3062. struct inode *inode = file_inode(file);
  3063. spin_lock(&inode->i_lock);
  3064. inode->i_state |= I_LINKABLE;
  3065. spin_unlock(&inode->i_lock);
  3066. }
  3067. out2:
  3068. mnt_drop_write(path.mnt);
  3069. out:
  3070. path_put(&path);
  3071. return error;
  3072. }
  3073. static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
  3074. {
  3075. struct path path;
  3076. int error = path_lookupat(nd, flags, &path);
  3077. if (!error) {
  3078. audit_inode(nd->name, path.dentry, 0);
  3079. error = vfs_open(&path, file, current_cred());
  3080. path_put(&path);
  3081. }
  3082. return error;
  3083. }
  3084. static struct file *path_openat(struct nameidata *nd,
  3085. const struct open_flags *op, unsigned flags)
  3086. {
  3087. const char *s;
  3088. struct file *file;
  3089. int opened = 0;
  3090. int error;
  3091. file = get_empty_filp();
  3092. if (IS_ERR(file))
  3093. return file;
  3094. file->f_flags = op->open_flag;
  3095. if (unlikely(file->f_flags & __O_TMPFILE)) {
  3096. error = do_tmpfile(nd, flags, op, file, &opened);
  3097. goto out2;
  3098. }
  3099. if (unlikely(file->f_flags & O_PATH)) {
  3100. error = do_o_path(nd, flags, file);
  3101. if (!error)
  3102. opened |= FILE_OPENED;
  3103. goto out2;
  3104. }
  3105. s = path_init(nd, flags);
  3106. if (IS_ERR(s)) {
  3107. put_filp(file);
  3108. return ERR_CAST(s);
  3109. }
  3110. while (!(error = link_path_walk(s, nd)) &&
  3111. (error = do_last(nd, file, op, &opened)) > 0) {
  3112. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  3113. s = trailing_symlink(nd);
  3114. if (IS_ERR(s)) {
  3115. error = PTR_ERR(s);
  3116. break;
  3117. }
  3118. }
  3119. terminate_walk(nd);
  3120. out2:
  3121. if (!(opened & FILE_OPENED)) {
  3122. BUG_ON(!error);
  3123. put_filp(file);
  3124. }
  3125. if (unlikely(error)) {
  3126. if (error == -EOPENSTALE) {
  3127. if (flags & LOOKUP_RCU)
  3128. error = -ECHILD;
  3129. else
  3130. error = -ESTALE;
  3131. }
  3132. file = ERR_PTR(error);
  3133. }
  3134. return file;
  3135. }
  3136. struct file *do_filp_open(int dfd, struct filename *pathname,
  3137. const struct open_flags *op)
  3138. {
  3139. struct nameidata nd;
  3140. int flags = op->lookup_flags;
  3141. struct file *filp;
  3142. set_nameidata(&nd, dfd, pathname);
  3143. filp = path_openat(&nd, op, flags | LOOKUP_RCU);
  3144. if (unlikely(filp == ERR_PTR(-ECHILD)))
  3145. filp = path_openat(&nd, op, flags);
  3146. if (unlikely(filp == ERR_PTR(-ESTALE)))
  3147. filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3148. restore_nameidata();
  3149. return filp;
  3150. }
  3151. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  3152. const char *name, const struct open_flags *op)
  3153. {
  3154. struct nameidata nd;
  3155. struct file *file;
  3156. struct filename *filename;
  3157. int flags = op->lookup_flags | LOOKUP_ROOT;
  3158. nd.root.mnt = mnt;
  3159. nd.root.dentry = dentry;
  3160. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  3161. return ERR_PTR(-ELOOP);
  3162. filename = getname_kernel(name);
  3163. if (IS_ERR(filename))
  3164. return ERR_CAST(filename);
  3165. set_nameidata(&nd, -1, filename);
  3166. file = path_openat(&nd, op, flags | LOOKUP_RCU);
  3167. if (unlikely(file == ERR_PTR(-ECHILD)))
  3168. file = path_openat(&nd, op, flags);
  3169. if (unlikely(file == ERR_PTR(-ESTALE)))
  3170. file = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3171. restore_nameidata();
  3172. putname(filename);
  3173. return file;
  3174. }
  3175. static struct dentry *filename_create(int dfd, struct filename *name,
  3176. struct path *path, unsigned int lookup_flags)
  3177. {
  3178. struct dentry *dentry = ERR_PTR(-EEXIST);
  3179. struct qstr last;
  3180. int type;
  3181. int err2;
  3182. int error;
  3183. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  3184. /*
  3185. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  3186. * other flags passed in are ignored!
  3187. */
  3188. lookup_flags &= LOOKUP_REVAL;
  3189. name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
  3190. if (IS_ERR(name))
  3191. return ERR_CAST(name);
  3192. /*
  3193. * Yucky last component or no last component at all?
  3194. * (foo/., foo/.., /////)
  3195. */
  3196. if (unlikely(type != LAST_NORM))
  3197. goto out;
  3198. /* don't fail immediately if it's r/o, at least try to report other errors */
  3199. err2 = mnt_want_write(path->mnt);
  3200. /*
  3201. * Do the final lookup.
  3202. */
  3203. lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  3204. inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
  3205. dentry = __lookup_hash(&last, path->dentry, lookup_flags);
  3206. if (IS_ERR(dentry))
  3207. goto unlock;
  3208. error = -EEXIST;
  3209. if (d_is_positive(dentry))
  3210. goto fail;
  3211. /*
  3212. * Special case - lookup gave negative, but... we had foo/bar/
  3213. * From the vfs_mknod() POV we just have a negative dentry -
  3214. * all is fine. Let's be bastards - you had / on the end, you've
  3215. * been asking for (non-existent) directory. -ENOENT for you.
  3216. */
  3217. if (unlikely(!is_dir && last.name[last.len])) {
  3218. error = -ENOENT;
  3219. goto fail;
  3220. }
  3221. if (unlikely(err2)) {
  3222. error = err2;
  3223. goto fail;
  3224. }
  3225. putname(name);
  3226. return dentry;
  3227. fail:
  3228. dput(dentry);
  3229. dentry = ERR_PTR(error);
  3230. unlock:
  3231. inode_unlock(path->dentry->d_inode);
  3232. if (!err2)
  3233. mnt_drop_write(path->mnt);
  3234. out:
  3235. path_put(path);
  3236. putname(name);
  3237. return dentry;
  3238. }
  3239. struct dentry *kern_path_create(int dfd, const char *pathname,
  3240. struct path *path, unsigned int lookup_flags)
  3241. {
  3242. return filename_create(dfd, getname_kernel(pathname),
  3243. path, lookup_flags);
  3244. }
  3245. EXPORT_SYMBOL(kern_path_create);
  3246. void done_path_create(struct path *path, struct dentry *dentry)
  3247. {
  3248. dput(dentry);
  3249. inode_unlock(path->dentry->d_inode);
  3250. mnt_drop_write(path->mnt);
  3251. path_put(path);
  3252. }
  3253. EXPORT_SYMBOL(done_path_create);
  3254. inline struct dentry *user_path_create(int dfd, const char __user *pathname,
  3255. struct path *path, unsigned int lookup_flags)
  3256. {
  3257. return filename_create(dfd, getname(pathname), path, lookup_flags);
  3258. }
  3259. EXPORT_SYMBOL(user_path_create);
  3260. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  3261. {
  3262. int error = may_create(dir, dentry);
  3263. if (error)
  3264. return error;
  3265. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  3266. return -EPERM;
  3267. if (!dir->i_op->mknod)
  3268. return -EPERM;
  3269. error = devcgroup_inode_mknod(mode, dev);
  3270. if (error)
  3271. return error;
  3272. error = security_inode_mknod(dir, dentry, mode, dev);
  3273. if (error)
  3274. return error;
  3275. error = dir->i_op->mknod(dir, dentry, mode, dev);
  3276. if (!error)
  3277. fsnotify_create(dir, dentry);
  3278. return error;
  3279. }
  3280. EXPORT_SYMBOL(vfs_mknod);
  3281. static int may_mknod(umode_t mode)
  3282. {
  3283. switch (mode & S_IFMT) {
  3284. case S_IFREG:
  3285. case S_IFCHR:
  3286. case S_IFBLK:
  3287. case S_IFIFO:
  3288. case S_IFSOCK:
  3289. case 0: /* zero mode translates to S_IFREG */
  3290. return 0;
  3291. case S_IFDIR:
  3292. return -EPERM;
  3293. default:
  3294. return -EINVAL;
  3295. }
  3296. }
  3297. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3298. unsigned, dev)
  3299. {
  3300. struct dentry *dentry;
  3301. struct path path;
  3302. int error;
  3303. unsigned int lookup_flags = 0;
  3304. error = may_mknod(mode);
  3305. if (error)
  3306. return error;
  3307. retry:
  3308. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3309. if (IS_ERR(dentry))
  3310. return PTR_ERR(dentry);
  3311. if (!IS_POSIXACL(path.dentry->d_inode))
  3312. mode &= ~current_umask();
  3313. error = security_path_mknod(&path, dentry, mode, dev);
  3314. if (error)
  3315. goto out;
  3316. switch (mode & S_IFMT) {
  3317. case 0: case S_IFREG:
  3318. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3319. if (!error)
  3320. ima_post_path_mknod(dentry);
  3321. break;
  3322. case S_IFCHR: case S_IFBLK:
  3323. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3324. new_decode_dev(dev));
  3325. break;
  3326. case S_IFIFO: case S_IFSOCK:
  3327. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3328. break;
  3329. }
  3330. out:
  3331. done_path_create(&path, dentry);
  3332. if (retry_estale(error, lookup_flags)) {
  3333. lookup_flags |= LOOKUP_REVAL;
  3334. goto retry;
  3335. }
  3336. return error;
  3337. }
  3338. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3339. {
  3340. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3341. }
  3342. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3343. {
  3344. int error = may_create(dir, dentry);
  3345. unsigned max_links = dir->i_sb->s_max_links;
  3346. if (error)
  3347. return error;
  3348. if (!dir->i_op->mkdir)
  3349. return -EPERM;
  3350. mode &= (S_IRWXUGO|S_ISVTX);
  3351. error = security_inode_mkdir(dir, dentry, mode);
  3352. if (error)
  3353. return error;
  3354. if (max_links && dir->i_nlink >= max_links)
  3355. return -EMLINK;
  3356. error = dir->i_op->mkdir(dir, dentry, mode);
  3357. if (!error)
  3358. fsnotify_mkdir(dir, dentry);
  3359. return error;
  3360. }
  3361. EXPORT_SYMBOL(vfs_mkdir);
  3362. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3363. {
  3364. struct dentry *dentry;
  3365. struct path path;
  3366. int error;
  3367. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3368. retry:
  3369. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3370. if (IS_ERR(dentry))
  3371. return PTR_ERR(dentry);
  3372. if (!IS_POSIXACL(path.dentry->d_inode))
  3373. mode &= ~current_umask();
  3374. error = security_path_mkdir(&path, dentry, mode);
  3375. if (!error)
  3376. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3377. done_path_create(&path, dentry);
  3378. if (retry_estale(error, lookup_flags)) {
  3379. lookup_flags |= LOOKUP_REVAL;
  3380. goto retry;
  3381. }
  3382. return error;
  3383. }
  3384. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3385. {
  3386. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3387. }
  3388. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3389. {
  3390. int error = may_delete(dir, dentry, 1);
  3391. if (error)
  3392. return error;
  3393. if (!dir->i_op->rmdir)
  3394. return -EPERM;
  3395. dget(dentry);
  3396. inode_lock(dentry->d_inode);
  3397. error = -EBUSY;
  3398. if (is_local_mountpoint(dentry))
  3399. goto out;
  3400. error = security_inode_rmdir(dir, dentry);
  3401. if (error)
  3402. goto out;
  3403. shrink_dcache_parent(dentry);
  3404. error = dir->i_op->rmdir(dir, dentry);
  3405. if (error)
  3406. goto out;
  3407. dentry->d_inode->i_flags |= S_DEAD;
  3408. dont_mount(dentry);
  3409. detach_mounts(dentry);
  3410. out:
  3411. inode_unlock(dentry->d_inode);
  3412. dput(dentry);
  3413. if (!error)
  3414. d_delete(dentry);
  3415. return error;
  3416. }
  3417. EXPORT_SYMBOL(vfs_rmdir);
  3418. static long do_rmdir(int dfd, const char __user *pathname)
  3419. {
  3420. int error = 0;
  3421. struct filename *name;
  3422. struct dentry *dentry;
  3423. struct path path;
  3424. struct qstr last;
  3425. int type;
  3426. unsigned int lookup_flags = 0;
  3427. retry:
  3428. name = user_path_parent(dfd, pathname,
  3429. &path, &last, &type, lookup_flags);
  3430. if (IS_ERR(name))
  3431. return PTR_ERR(name);
  3432. switch (type) {
  3433. case LAST_DOTDOT:
  3434. error = -ENOTEMPTY;
  3435. goto exit1;
  3436. case LAST_DOT:
  3437. error = -EINVAL;
  3438. goto exit1;
  3439. case LAST_ROOT:
  3440. error = -EBUSY;
  3441. goto exit1;
  3442. }
  3443. error = mnt_want_write(path.mnt);
  3444. if (error)
  3445. goto exit1;
  3446. inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
  3447. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3448. error = PTR_ERR(dentry);
  3449. if (IS_ERR(dentry))
  3450. goto exit2;
  3451. if (!dentry->d_inode) {
  3452. error = -ENOENT;
  3453. goto exit3;
  3454. }
  3455. error = security_path_rmdir(&path, dentry);
  3456. if (error)
  3457. goto exit3;
  3458. error = vfs_rmdir(path.dentry->d_inode, dentry);
  3459. exit3:
  3460. dput(dentry);
  3461. exit2:
  3462. inode_unlock(path.dentry->d_inode);
  3463. mnt_drop_write(path.mnt);
  3464. exit1:
  3465. path_put(&path);
  3466. putname(name);
  3467. if (retry_estale(error, lookup_flags)) {
  3468. lookup_flags |= LOOKUP_REVAL;
  3469. goto retry;
  3470. }
  3471. return error;
  3472. }
  3473. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3474. {
  3475. return do_rmdir(AT_FDCWD, pathname);
  3476. }
  3477. /**
  3478. * vfs_unlink - unlink a filesystem object
  3479. * @dir: parent directory
  3480. * @dentry: victim
  3481. * @delegated_inode: returns victim inode, if the inode is delegated.
  3482. *
  3483. * The caller must hold dir->i_mutex.
  3484. *
  3485. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3486. * return a reference to the inode in delegated_inode. The caller
  3487. * should then break the delegation on that inode and retry. Because
  3488. * breaking a delegation may take a long time, the caller should drop
  3489. * dir->i_mutex before doing so.
  3490. *
  3491. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3492. * be appropriate for callers that expect the underlying filesystem not
  3493. * to be NFS exported.
  3494. */
  3495. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3496. {
  3497. struct inode *target = dentry->d_inode;
  3498. int error = may_delete(dir, dentry, 0);
  3499. if (error)
  3500. return error;
  3501. if (!dir->i_op->unlink)
  3502. return -EPERM;
  3503. inode_lock(target);
  3504. if (is_local_mountpoint(dentry))
  3505. error = -EBUSY;
  3506. else {
  3507. error = security_inode_unlink(dir, dentry);
  3508. if (!error) {
  3509. error = try_break_deleg(target, delegated_inode);
  3510. if (error)
  3511. goto out;
  3512. error = dir->i_op->unlink(dir, dentry);
  3513. if (!error) {
  3514. dont_mount(dentry);
  3515. detach_mounts(dentry);
  3516. }
  3517. }
  3518. }
  3519. out:
  3520. inode_unlock(target);
  3521. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3522. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3523. fsnotify_link_count(target);
  3524. d_delete(dentry);
  3525. }
  3526. return error;
  3527. }
  3528. EXPORT_SYMBOL(vfs_unlink);
  3529. /*
  3530. * Make sure that the actual truncation of the file will occur outside its
  3531. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3532. * writeout happening, and we don't want to prevent access to the directory
  3533. * while waiting on the I/O.
  3534. */
  3535. static long do_unlinkat(int dfd, const char __user *pathname)
  3536. {
  3537. int error;
  3538. struct filename *name;
  3539. struct dentry *dentry;
  3540. struct path path;
  3541. struct qstr last;
  3542. int type;
  3543. struct inode *inode = NULL;
  3544. struct inode *delegated_inode = NULL;
  3545. unsigned int lookup_flags = 0;
  3546. retry:
  3547. name = user_path_parent(dfd, pathname,
  3548. &path, &last, &type, lookup_flags);
  3549. if (IS_ERR(name))
  3550. return PTR_ERR(name);
  3551. error = -EISDIR;
  3552. if (type != LAST_NORM)
  3553. goto exit1;
  3554. error = mnt_want_write(path.mnt);
  3555. if (error)
  3556. goto exit1;
  3557. retry_deleg:
  3558. inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
  3559. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3560. error = PTR_ERR(dentry);
  3561. if (!IS_ERR(dentry)) {
  3562. /* Why not before? Because we want correct error value */
  3563. if (last.name[last.len])
  3564. goto slashes;
  3565. inode = dentry->d_inode;
  3566. if (d_is_negative(dentry))
  3567. goto slashes;
  3568. ihold(inode);
  3569. error = security_path_unlink(&path, dentry);
  3570. if (error)
  3571. goto exit2;
  3572. error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
  3573. exit2:
  3574. dput(dentry);
  3575. }
  3576. inode_unlock(path.dentry->d_inode);
  3577. if (inode)
  3578. iput(inode); /* truncate the inode here */
  3579. inode = NULL;
  3580. if (delegated_inode) {
  3581. error = break_deleg_wait(&delegated_inode);
  3582. if (!error)
  3583. goto retry_deleg;
  3584. }
  3585. mnt_drop_write(path.mnt);
  3586. exit1:
  3587. path_put(&path);
  3588. putname(name);
  3589. if (retry_estale(error, lookup_flags)) {
  3590. lookup_flags |= LOOKUP_REVAL;
  3591. inode = NULL;
  3592. goto retry;
  3593. }
  3594. return error;
  3595. slashes:
  3596. if (d_is_negative(dentry))
  3597. error = -ENOENT;
  3598. else if (d_is_dir(dentry))
  3599. error = -EISDIR;
  3600. else
  3601. error = -ENOTDIR;
  3602. goto exit2;
  3603. }
  3604. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3605. {
  3606. if ((flag & ~AT_REMOVEDIR) != 0)
  3607. return -EINVAL;
  3608. if (flag & AT_REMOVEDIR)
  3609. return do_rmdir(dfd, pathname);
  3610. return do_unlinkat(dfd, pathname);
  3611. }
  3612. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3613. {
  3614. return do_unlinkat(AT_FDCWD, pathname);
  3615. }
  3616. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3617. {
  3618. int error = may_create(dir, dentry);
  3619. if (error)
  3620. return error;
  3621. if (!dir->i_op->symlink)
  3622. return -EPERM;
  3623. error = security_inode_symlink(dir, dentry, oldname);
  3624. if (error)
  3625. return error;
  3626. error = dir->i_op->symlink(dir, dentry, oldname);
  3627. if (!error)
  3628. fsnotify_create(dir, dentry);
  3629. return error;
  3630. }
  3631. EXPORT_SYMBOL(vfs_symlink);
  3632. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3633. int, newdfd, const char __user *, newname)
  3634. {
  3635. int error;
  3636. struct filename *from;
  3637. struct dentry *dentry;
  3638. struct path path;
  3639. unsigned int lookup_flags = 0;
  3640. from = getname(oldname);
  3641. if (IS_ERR(from))
  3642. return PTR_ERR(from);
  3643. retry:
  3644. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3645. error = PTR_ERR(dentry);
  3646. if (IS_ERR(dentry))
  3647. goto out_putname;
  3648. error = security_path_symlink(&path, dentry, from->name);
  3649. if (!error)
  3650. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3651. done_path_create(&path, dentry);
  3652. if (retry_estale(error, lookup_flags)) {
  3653. lookup_flags |= LOOKUP_REVAL;
  3654. goto retry;
  3655. }
  3656. out_putname:
  3657. putname(from);
  3658. return error;
  3659. }
  3660. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3661. {
  3662. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3663. }
  3664. /**
  3665. * vfs_link - create a new link
  3666. * @old_dentry: object to be linked
  3667. * @dir: new parent
  3668. * @new_dentry: where to create the new link
  3669. * @delegated_inode: returns inode needing a delegation break
  3670. *
  3671. * The caller must hold dir->i_mutex
  3672. *
  3673. * If vfs_link discovers a delegation on the to-be-linked file in need
  3674. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3675. * inode in delegated_inode. The caller should then break the delegation
  3676. * and retry. Because breaking a delegation may take a long time, the
  3677. * caller should drop the i_mutex before doing so.
  3678. *
  3679. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3680. * be appropriate for callers that expect the underlying filesystem not
  3681. * to be NFS exported.
  3682. */
  3683. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3684. {
  3685. struct inode *inode = old_dentry->d_inode;
  3686. unsigned max_links = dir->i_sb->s_max_links;
  3687. int error;
  3688. if (!inode)
  3689. return -ENOENT;
  3690. error = may_create(dir, new_dentry);
  3691. if (error)
  3692. return error;
  3693. if (dir->i_sb != inode->i_sb)
  3694. return -EXDEV;
  3695. /*
  3696. * A link to an append-only or immutable file cannot be created.
  3697. */
  3698. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3699. return -EPERM;
  3700. if (!dir->i_op->link)
  3701. return -EPERM;
  3702. if (S_ISDIR(inode->i_mode))
  3703. return -EPERM;
  3704. error = security_inode_link(old_dentry, dir, new_dentry);
  3705. if (error)
  3706. return error;
  3707. inode_lock(inode);
  3708. /* Make sure we don't allow creating hardlink to an unlinked file */
  3709. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3710. error = -ENOENT;
  3711. else if (max_links && inode->i_nlink >= max_links)
  3712. error = -EMLINK;
  3713. else {
  3714. error = try_break_deleg(inode, delegated_inode);
  3715. if (!error)
  3716. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3717. }
  3718. if (!error && (inode->i_state & I_LINKABLE)) {
  3719. spin_lock(&inode->i_lock);
  3720. inode->i_state &= ~I_LINKABLE;
  3721. spin_unlock(&inode->i_lock);
  3722. }
  3723. inode_unlock(inode);
  3724. if (!error)
  3725. fsnotify_link(dir, inode, new_dentry);
  3726. return error;
  3727. }
  3728. EXPORT_SYMBOL(vfs_link);
  3729. /*
  3730. * Hardlinks are often used in delicate situations. We avoid
  3731. * security-related surprises by not following symlinks on the
  3732. * newname. --KAB
  3733. *
  3734. * We don't follow them on the oldname either to be compatible
  3735. * with linux 2.0, and to avoid hard-linking to directories
  3736. * and other special files. --ADM
  3737. */
  3738. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3739. int, newdfd, const char __user *, newname, int, flags)
  3740. {
  3741. struct dentry *new_dentry;
  3742. struct path old_path, new_path;
  3743. struct inode *delegated_inode = NULL;
  3744. int how = 0;
  3745. int error;
  3746. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3747. return -EINVAL;
  3748. /*
  3749. * To use null names we require CAP_DAC_READ_SEARCH
  3750. * This ensures that not everyone will be able to create
  3751. * handlink using the passed filedescriptor.
  3752. */
  3753. if (flags & AT_EMPTY_PATH) {
  3754. if (!capable(CAP_DAC_READ_SEARCH))
  3755. return -ENOENT;
  3756. how = LOOKUP_EMPTY;
  3757. }
  3758. if (flags & AT_SYMLINK_FOLLOW)
  3759. how |= LOOKUP_FOLLOW;
  3760. retry:
  3761. error = user_path_at(olddfd, oldname, how, &old_path);
  3762. if (error)
  3763. return error;
  3764. new_dentry = user_path_create(newdfd, newname, &new_path,
  3765. (how & LOOKUP_REVAL));
  3766. error = PTR_ERR(new_dentry);
  3767. if (IS_ERR(new_dentry))
  3768. goto out;
  3769. error = -EXDEV;
  3770. if (old_path.mnt != new_path.mnt)
  3771. goto out_dput;
  3772. error = may_linkat(&old_path);
  3773. if (unlikely(error))
  3774. goto out_dput;
  3775. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3776. if (error)
  3777. goto out_dput;
  3778. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3779. out_dput:
  3780. done_path_create(&new_path, new_dentry);
  3781. if (delegated_inode) {
  3782. error = break_deleg_wait(&delegated_inode);
  3783. if (!error) {
  3784. path_put(&old_path);
  3785. goto retry;
  3786. }
  3787. }
  3788. if (retry_estale(error, how)) {
  3789. path_put(&old_path);
  3790. how |= LOOKUP_REVAL;
  3791. goto retry;
  3792. }
  3793. out:
  3794. path_put(&old_path);
  3795. return error;
  3796. }
  3797. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3798. {
  3799. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3800. }
  3801. /**
  3802. * vfs_rename - rename a filesystem object
  3803. * @old_dir: parent of source
  3804. * @old_dentry: source
  3805. * @new_dir: parent of destination
  3806. * @new_dentry: destination
  3807. * @delegated_inode: returns an inode needing a delegation break
  3808. * @flags: rename flags
  3809. *
  3810. * The caller must hold multiple mutexes--see lock_rename()).
  3811. *
  3812. * If vfs_rename discovers a delegation in need of breaking at either
  3813. * the source or destination, it will return -EWOULDBLOCK and return a
  3814. * reference to the inode in delegated_inode. The caller should then
  3815. * break the delegation and retry. Because breaking a delegation may
  3816. * take a long time, the caller should drop all locks before doing
  3817. * so.
  3818. *
  3819. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3820. * be appropriate for callers that expect the underlying filesystem not
  3821. * to be NFS exported.
  3822. *
  3823. * The worst of all namespace operations - renaming directory. "Perverted"
  3824. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3825. * Problems:
  3826. * a) we can get into loop creation.
  3827. * b) race potential - two innocent renames can create a loop together.
  3828. * That's where 4.4 screws up. Current fix: serialization on
  3829. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3830. * story.
  3831. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3832. * and source (if it is not a directory).
  3833. * And that - after we got ->i_mutex on parents (until then we don't know
  3834. * whether the target exists). Solution: try to be smart with locking
  3835. * order for inodes. We rely on the fact that tree topology may change
  3836. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3837. * move will be locked. Thus we can rank directories by the tree
  3838. * (ancestors first) and rank all non-directories after them.
  3839. * That works since everybody except rename does "lock parent, lookup,
  3840. * lock child" and rename is under ->s_vfs_rename_mutex.
  3841. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3842. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3843. * we'd better make sure that there's no link(2) for them.
  3844. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3845. * we are removing the target. Solution: we will have to grab ->i_mutex
  3846. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3847. * ->i_mutex on parents, which works but leads to some truly excessive
  3848. * locking].
  3849. */
  3850. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3851. struct inode *new_dir, struct dentry *new_dentry,
  3852. struct inode **delegated_inode, unsigned int flags)
  3853. {
  3854. int error;
  3855. bool is_dir = d_is_dir(old_dentry);
  3856. const unsigned char *old_name;
  3857. struct inode *source = old_dentry->d_inode;
  3858. struct inode *target = new_dentry->d_inode;
  3859. bool new_is_dir = false;
  3860. unsigned max_links = new_dir->i_sb->s_max_links;
  3861. /*
  3862. * Check source == target.
  3863. * On overlayfs need to look at underlying inodes.
  3864. */
  3865. if (vfs_select_inode(old_dentry, 0) == vfs_select_inode(new_dentry, 0))
  3866. return 0;
  3867. error = may_delete(old_dir, old_dentry, is_dir);
  3868. if (error)
  3869. return error;
  3870. if (!target) {
  3871. error = may_create(new_dir, new_dentry);
  3872. } else {
  3873. new_is_dir = d_is_dir(new_dentry);
  3874. if (!(flags & RENAME_EXCHANGE))
  3875. error = may_delete(new_dir, new_dentry, is_dir);
  3876. else
  3877. error = may_delete(new_dir, new_dentry, new_is_dir);
  3878. }
  3879. if (error)
  3880. return error;
  3881. if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
  3882. return -EPERM;
  3883. if (flags && !old_dir->i_op->rename2)
  3884. return -EINVAL;
  3885. /*
  3886. * If we are going to change the parent - check write permissions,
  3887. * we'll need to flip '..'.
  3888. */
  3889. if (new_dir != old_dir) {
  3890. if (is_dir) {
  3891. error = inode_permission(source, MAY_WRITE);
  3892. if (error)
  3893. return error;
  3894. }
  3895. if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  3896. error = inode_permission(target, MAY_WRITE);
  3897. if (error)
  3898. return error;
  3899. }
  3900. }
  3901. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  3902. flags);
  3903. if (error)
  3904. return error;
  3905. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3906. dget(new_dentry);
  3907. if (!is_dir || (flags & RENAME_EXCHANGE))
  3908. lock_two_nondirectories(source, target);
  3909. else if (target)
  3910. inode_lock(target);
  3911. error = -EBUSY;
  3912. if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
  3913. goto out;
  3914. if (max_links && new_dir != old_dir) {
  3915. error = -EMLINK;
  3916. if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
  3917. goto out;
  3918. if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  3919. old_dir->i_nlink >= max_links)
  3920. goto out;
  3921. }
  3922. if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  3923. shrink_dcache_parent(new_dentry);
  3924. if (!is_dir) {
  3925. error = try_break_deleg(source, delegated_inode);
  3926. if (error)
  3927. goto out;
  3928. }
  3929. if (target && !new_is_dir) {
  3930. error = try_break_deleg(target, delegated_inode);
  3931. if (error)
  3932. goto out;
  3933. }
  3934. if (!old_dir->i_op->rename2) {
  3935. error = old_dir->i_op->rename(old_dir, old_dentry,
  3936. new_dir, new_dentry);
  3937. } else {
  3938. WARN_ON(old_dir->i_op->rename != NULL);
  3939. error = old_dir->i_op->rename2(old_dir, old_dentry,
  3940. new_dir, new_dentry, flags);
  3941. }
  3942. if (error)
  3943. goto out;
  3944. if (!(flags & RENAME_EXCHANGE) && target) {
  3945. if (is_dir)
  3946. target->i_flags |= S_DEAD;
  3947. dont_mount(new_dentry);
  3948. detach_mounts(new_dentry);
  3949. }
  3950. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  3951. if (!(flags & RENAME_EXCHANGE))
  3952. d_move(old_dentry, new_dentry);
  3953. else
  3954. d_exchange(old_dentry, new_dentry);
  3955. }
  3956. out:
  3957. if (!is_dir || (flags & RENAME_EXCHANGE))
  3958. unlock_two_nondirectories(source, target);
  3959. else if (target)
  3960. inode_unlock(target);
  3961. dput(new_dentry);
  3962. if (!error) {
  3963. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3964. !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  3965. if (flags & RENAME_EXCHANGE) {
  3966. fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  3967. new_is_dir, NULL, new_dentry);
  3968. }
  3969. }
  3970. fsnotify_oldname_free(old_name);
  3971. return error;
  3972. }
  3973. EXPORT_SYMBOL(vfs_rename);
  3974. SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  3975. int, newdfd, const char __user *, newname, unsigned int, flags)
  3976. {
  3977. struct dentry *old_dentry, *new_dentry;
  3978. struct dentry *trap;
  3979. struct path old_path, new_path;
  3980. struct qstr old_last, new_last;
  3981. int old_type, new_type;
  3982. struct inode *delegated_inode = NULL;
  3983. struct filename *from;
  3984. struct filename *to;
  3985. unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
  3986. bool should_retry = false;
  3987. int error;
  3988. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3989. return -EINVAL;
  3990. if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
  3991. (flags & RENAME_EXCHANGE))
  3992. return -EINVAL;
  3993. if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
  3994. return -EPERM;
  3995. if (flags & RENAME_EXCHANGE)
  3996. target_flags = 0;
  3997. retry:
  3998. from = user_path_parent(olddfd, oldname,
  3999. &old_path, &old_last, &old_type, lookup_flags);
  4000. if (IS_ERR(from)) {
  4001. error = PTR_ERR(from);
  4002. goto exit;
  4003. }
  4004. to = user_path_parent(newdfd, newname,
  4005. &new_path, &new_last, &new_type, lookup_flags);
  4006. if (IS_ERR(to)) {
  4007. error = PTR_ERR(to);
  4008. goto exit1;
  4009. }
  4010. error = -EXDEV;
  4011. if (old_path.mnt != new_path.mnt)
  4012. goto exit2;
  4013. error = -EBUSY;
  4014. if (old_type != LAST_NORM)
  4015. goto exit2;
  4016. if (flags & RENAME_NOREPLACE)
  4017. error = -EEXIST;
  4018. if (new_type != LAST_NORM)
  4019. goto exit2;
  4020. error = mnt_want_write(old_path.mnt);
  4021. if (error)
  4022. goto exit2;
  4023. retry_deleg:
  4024. trap = lock_rename(new_path.dentry, old_path.dentry);
  4025. old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
  4026. error = PTR_ERR(old_dentry);
  4027. if (IS_ERR(old_dentry))
  4028. goto exit3;
  4029. /* source must exist */
  4030. error = -ENOENT;
  4031. if (d_is_negative(old_dentry))
  4032. goto exit4;
  4033. new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
  4034. error = PTR_ERR(new_dentry);
  4035. if (IS_ERR(new_dentry))
  4036. goto exit4;
  4037. error = -EEXIST;
  4038. if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  4039. goto exit5;
  4040. if (flags & RENAME_EXCHANGE) {
  4041. error = -ENOENT;
  4042. if (d_is_negative(new_dentry))
  4043. goto exit5;
  4044. if (!d_is_dir(new_dentry)) {
  4045. error = -ENOTDIR;
  4046. if (new_last.name[new_last.len])
  4047. goto exit5;
  4048. }
  4049. }
  4050. /* unless the source is a directory trailing slashes give -ENOTDIR */
  4051. if (!d_is_dir(old_dentry)) {
  4052. error = -ENOTDIR;
  4053. if (old_last.name[old_last.len])
  4054. goto exit5;
  4055. if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
  4056. goto exit5;
  4057. }
  4058. /* source should not be ancestor of target */
  4059. error = -EINVAL;
  4060. if (old_dentry == trap)
  4061. goto exit5;
  4062. /* target should not be an ancestor of source */
  4063. if (!(flags & RENAME_EXCHANGE))
  4064. error = -ENOTEMPTY;
  4065. if (new_dentry == trap)
  4066. goto exit5;
  4067. error = security_path_rename(&old_path, old_dentry,
  4068. &new_path, new_dentry, flags);
  4069. if (error)
  4070. goto exit5;
  4071. error = vfs_rename(old_path.dentry->d_inode, old_dentry,
  4072. new_path.dentry->d_inode, new_dentry,
  4073. &delegated_inode, flags);
  4074. exit5:
  4075. dput(new_dentry);
  4076. exit4:
  4077. dput(old_dentry);
  4078. exit3:
  4079. unlock_rename(new_path.dentry, old_path.dentry);
  4080. if (delegated_inode) {
  4081. error = break_deleg_wait(&delegated_inode);
  4082. if (!error)
  4083. goto retry_deleg;
  4084. }
  4085. mnt_drop_write(old_path.mnt);
  4086. exit2:
  4087. if (retry_estale(error, lookup_flags))
  4088. should_retry = true;
  4089. path_put(&new_path);
  4090. putname(to);
  4091. exit1:
  4092. path_put(&old_path);
  4093. putname(from);
  4094. if (should_retry) {
  4095. should_retry = false;
  4096. lookup_flags |= LOOKUP_REVAL;
  4097. goto retry;
  4098. }
  4099. exit:
  4100. return error;
  4101. }
  4102. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  4103. int, newdfd, const char __user *, newname)
  4104. {
  4105. return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  4106. }
  4107. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  4108. {
  4109. return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  4110. }
  4111. int vfs_whiteout(struct inode *dir, struct dentry *dentry)
  4112. {
  4113. int error = may_create(dir, dentry);
  4114. if (error)
  4115. return error;
  4116. if (!dir->i_op->mknod)
  4117. return -EPERM;
  4118. return dir->i_op->mknod(dir, dentry,
  4119. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  4120. }
  4121. EXPORT_SYMBOL(vfs_whiteout);
  4122. int readlink_copy(char __user *buffer, int buflen, const char *link)
  4123. {
  4124. int len = PTR_ERR(link);
  4125. if (IS_ERR(link))
  4126. goto out;
  4127. len = strlen(link);
  4128. if (len > (unsigned) buflen)
  4129. len = buflen;
  4130. if (copy_to_user(buffer, link, len))
  4131. len = -EFAULT;
  4132. out:
  4133. return len;
  4134. }
  4135. /*
  4136. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  4137. * have ->get_link() not calling nd_jump_link(). Using (or not using) it
  4138. * for any given inode is up to filesystem.
  4139. */
  4140. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4141. {
  4142. DEFINE_DELAYED_CALL(done);
  4143. struct inode *inode = d_inode(dentry);
  4144. const char *link = inode->i_link;
  4145. int res;
  4146. if (!link) {
  4147. link = inode->i_op->get_link(dentry, inode, &done);
  4148. if (IS_ERR(link))
  4149. return PTR_ERR(link);
  4150. }
  4151. res = readlink_copy(buffer, buflen, link);
  4152. do_delayed_call(&done);
  4153. return res;
  4154. }
  4155. EXPORT_SYMBOL(generic_readlink);
  4156. /* get the link contents into pagecache */
  4157. const char *page_get_link(struct dentry *dentry, struct inode *inode,
  4158. struct delayed_call *callback)
  4159. {
  4160. char *kaddr;
  4161. struct page *page;
  4162. struct address_space *mapping = inode->i_mapping;
  4163. if (!dentry) {
  4164. page = find_get_page(mapping, 0);
  4165. if (!page)
  4166. return ERR_PTR(-ECHILD);
  4167. if (!PageUptodate(page)) {
  4168. put_page(page);
  4169. return ERR_PTR(-ECHILD);
  4170. }
  4171. } else {
  4172. page = read_mapping_page(mapping, 0, NULL);
  4173. if (IS_ERR(page))
  4174. return (char*)page;
  4175. }
  4176. set_delayed_call(callback, page_put_link, page);
  4177. BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
  4178. kaddr = page_address(page);
  4179. nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
  4180. return kaddr;
  4181. }
  4182. EXPORT_SYMBOL(page_get_link);
  4183. void page_put_link(void *arg)
  4184. {
  4185. put_page(arg);
  4186. }
  4187. EXPORT_SYMBOL(page_put_link);
  4188. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4189. {
  4190. DEFINE_DELAYED_CALL(done);
  4191. int res = readlink_copy(buffer, buflen,
  4192. page_get_link(dentry, d_inode(dentry),
  4193. &done));
  4194. do_delayed_call(&done);
  4195. return res;
  4196. }
  4197. EXPORT_SYMBOL(page_readlink);
  4198. /*
  4199. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  4200. */
  4201. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  4202. {
  4203. struct address_space *mapping = inode->i_mapping;
  4204. struct page *page;
  4205. void *fsdata;
  4206. int err;
  4207. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  4208. if (nofs)
  4209. flags |= AOP_FLAG_NOFS;
  4210. retry:
  4211. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  4212. flags, &page, &fsdata);
  4213. if (err)
  4214. goto fail;
  4215. memcpy(page_address(page), symname, len-1);
  4216. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  4217. page, fsdata);
  4218. if (err < 0)
  4219. goto fail;
  4220. if (err < len-1)
  4221. goto retry;
  4222. mark_inode_dirty(inode);
  4223. return 0;
  4224. fail:
  4225. return err;
  4226. }
  4227. EXPORT_SYMBOL(__page_symlink);
  4228. int page_symlink(struct inode *inode, const char *symname, int len)
  4229. {
  4230. return __page_symlink(inode, symname, len,
  4231. !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
  4232. }
  4233. EXPORT_SYMBOL(page_symlink);
  4234. const struct inode_operations page_symlink_inode_operations = {
  4235. .readlink = generic_readlink,
  4236. .get_link = page_get_link,
  4237. };
  4238. EXPORT_SYMBOL(page_symlink_inode_operations);