namei.c 113 KB

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