namei.c 112 KB

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