namei.c 115 KB

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