namei.c 111 KB

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