namei.c 111 KB

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