namei.c 97 KB

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