namei.c 103 KB

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