namei.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342
  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/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mount.h>
  26. #include <linux/audit.h>
  27. #include <linux/capability.h>
  28. #include <linux/file.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/device_cgroup.h>
  31. #include <linux/fs_struct.h>
  32. #include <asm/uaccess.h>
  33. #include "internal.h"
  34. /* [Feb-1997 T. Schoebel-Theuer]
  35. * Fundamental changes in the pathname lookup mechanisms (namei)
  36. * were necessary because of omirr. The reason is that omirr needs
  37. * to know the _real_ pathname, not the user-supplied one, in case
  38. * of symlinks (and also when transname replacements occur).
  39. *
  40. * The new code replaces the old recursive symlink resolution with
  41. * an iterative one (in case of non-nested symlink chains). It does
  42. * this with calls to <fs>_follow_link().
  43. * As a side effect, dir_namei(), _namei() and follow_link() are now
  44. * replaced with a single function lookup_dentry() that can handle all
  45. * the special cases of the former code.
  46. *
  47. * With the new dcache, the pathname is stored at each inode, at least as
  48. * long as the refcount of the inode is positive. As a side effect, the
  49. * size of the dcache depends on the inode cache and thus is dynamic.
  50. *
  51. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  52. * resolution to correspond with current state of the code.
  53. *
  54. * Note that the symlink resolution is not *completely* iterative.
  55. * There is still a significant amount of tail- and mid- recursion in
  56. * the algorithm. Also, note that <fs>_readlink() is not used in
  57. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  58. * may return different results than <fs>_follow_link(). Many virtual
  59. * filesystems (including /proc) exhibit this behavior.
  60. */
  61. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  62. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  63. * and the name already exists in form of a symlink, try to create the new
  64. * name indicated by the symlink. The old code always complained that the
  65. * name already exists, due to not following the symlink even if its target
  66. * is nonexistent. The new semantics affects also mknod() and link() when
  67. * the name is a symlink pointing to a non-existent name.
  68. *
  69. * I don't know which semantics is the right one, since I have no access
  70. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  71. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  72. * "old" one. Personally, I think the new semantics is much more logical.
  73. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  74. * file does succeed in both HP-UX and SunOs, but not in Solaris
  75. * and in the old Linux semantics.
  76. */
  77. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  78. * semantics. See the comments in "open_namei" and "do_link" below.
  79. *
  80. * [10-Sep-98 Alan Modra] Another symlink change.
  81. */
  82. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  83. * inside the path - always follow.
  84. * in the last component in creation/removal/renaming - never follow.
  85. * if LOOKUP_FOLLOW passed - follow.
  86. * if the pathname has trailing slashes - follow.
  87. * otherwise - don't follow.
  88. * (applied in that order).
  89. *
  90. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  91. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  92. * During the 2.4 we need to fix the userland stuff depending on it -
  93. * hopefully we will be able to get rid of that wart in 2.5. So far only
  94. * XEmacs seems to be relying on it...
  95. */
  96. /*
  97. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  98. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  99. * any extra contention...
  100. */
  101. /* In order to reduce some races, while at the same time doing additional
  102. * checking and hopefully speeding things up, we copy filenames to the
  103. * kernel data space before using them..
  104. *
  105. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  106. * PATH_MAX includes the nul terminator --RR.
  107. */
  108. static int do_getname(const char __user *filename, char *page)
  109. {
  110. int retval;
  111. unsigned long len = PATH_MAX;
  112. if (!segment_eq(get_fs(), KERNEL_DS)) {
  113. if ((unsigned long) filename >= TASK_SIZE)
  114. return -EFAULT;
  115. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  116. len = TASK_SIZE - (unsigned long) filename;
  117. }
  118. retval = strncpy_from_user(page, filename, len);
  119. if (retval > 0) {
  120. if (retval < len)
  121. return 0;
  122. return -ENAMETOOLONG;
  123. } else if (!retval)
  124. retval = -ENOENT;
  125. return retval;
  126. }
  127. static char *getname_flags(const char __user * filename, int flags)
  128. {
  129. char *tmp, *result;
  130. result = ERR_PTR(-ENOMEM);
  131. tmp = __getname();
  132. if (tmp) {
  133. int retval = do_getname(filename, tmp);
  134. result = tmp;
  135. if (retval < 0) {
  136. if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. }
  142. audit_getname(result);
  143. return result;
  144. }
  145. char *getname(const char __user * filename)
  146. {
  147. return getname_flags(filename, 0);
  148. }
  149. #ifdef CONFIG_AUDITSYSCALL
  150. void putname(const char *name)
  151. {
  152. if (unlikely(!audit_dummy_context()))
  153. audit_putname(name);
  154. else
  155. __putname(name);
  156. }
  157. EXPORT_SYMBOL(putname);
  158. #endif
  159. /*
  160. * This does basic POSIX ACL permission checking
  161. */
  162. static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
  163. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  164. {
  165. umode_t mode = inode->i_mode;
  166. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  167. if (current_user_ns() != inode_userns(inode))
  168. goto other_perms;
  169. if (current_fsuid() == inode->i_uid)
  170. mode >>= 6;
  171. else {
  172. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  173. int error = check_acl(inode, mask, flags);
  174. if (error != -EAGAIN)
  175. return error;
  176. }
  177. if (in_group_p(inode->i_gid))
  178. mode >>= 3;
  179. }
  180. other_perms:
  181. /*
  182. * If the DACs are ok we don't need any capability check.
  183. */
  184. if ((mask & ~mode) == 0)
  185. return 0;
  186. return -EACCES;
  187. }
  188. /**
  189. * generic_permission - check for access rights on a Posix-like filesystem
  190. * @inode: inode to check access rights for
  191. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  192. * @check_acl: optional callback to check for Posix ACLs
  193. * @flags: IPERM_FLAG_ flags.
  194. *
  195. * Used to check for read/write/execute permissions on a file.
  196. * We use "fsuid" for this, letting us set arbitrary permissions
  197. * for filesystem access without changing the "normal" uids which
  198. * are used for other things.
  199. *
  200. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  201. * request cannot be satisfied (eg. requires blocking or too much complexity).
  202. * It would then be called again in ref-walk mode.
  203. */
  204. int generic_permission(struct inode *inode, int mask, unsigned int flags,
  205. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  206. {
  207. int ret;
  208. /*
  209. * Do the basic POSIX ACL permission checks.
  210. */
  211. ret = acl_permission_check(inode, mask, flags, check_acl);
  212. if (ret != -EACCES)
  213. return ret;
  214. /*
  215. * Read/write DACs are always overridable.
  216. * Executable DACs are overridable if at least one exec bit is set.
  217. */
  218. if (!(mask & MAY_EXEC) || execute_ok(inode))
  219. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  220. return 0;
  221. /*
  222. * Searching includes executable on directories, else just read.
  223. */
  224. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  225. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  226. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  227. return 0;
  228. return -EACCES;
  229. }
  230. /**
  231. * inode_permission - check for access rights to a given inode
  232. * @inode: inode to check permission on
  233. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  234. *
  235. * Used to check for read/write/execute permissions on an inode.
  236. * We use "fsuid" for this, letting us set arbitrary permissions
  237. * for filesystem access without changing the "normal" uids which
  238. * are used for other things.
  239. */
  240. int inode_permission(struct inode *inode, int mask)
  241. {
  242. int retval;
  243. if (mask & MAY_WRITE) {
  244. umode_t mode = inode->i_mode;
  245. /*
  246. * Nobody gets write access to a read-only fs.
  247. */
  248. if (IS_RDONLY(inode) &&
  249. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  250. return -EROFS;
  251. /*
  252. * Nobody gets write access to an immutable file.
  253. */
  254. if (IS_IMMUTABLE(inode))
  255. return -EACCES;
  256. }
  257. if (inode->i_op->permission)
  258. retval = inode->i_op->permission(inode, mask, 0);
  259. else
  260. retval = generic_permission(inode, mask, 0,
  261. inode->i_op->check_acl);
  262. if (retval)
  263. return retval;
  264. retval = devcgroup_inode_permission(inode, mask);
  265. if (retval)
  266. return retval;
  267. return security_inode_permission(inode, mask);
  268. }
  269. /**
  270. * file_permission - check for additional access rights to a given file
  271. * @file: file to check access rights for
  272. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  273. *
  274. * Used to check for read/write/execute permissions on an already opened
  275. * file.
  276. *
  277. * Note:
  278. * Do not use this function in new code. All access checks should
  279. * be done using inode_permission().
  280. */
  281. int file_permission(struct file *file, int mask)
  282. {
  283. return inode_permission(file->f_path.dentry->d_inode, mask);
  284. }
  285. /*
  286. * get_write_access() gets write permission for a file.
  287. * put_write_access() releases this write permission.
  288. * This is used for regular files.
  289. * We cannot support write (and maybe mmap read-write shared) accesses and
  290. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  291. * can have the following values:
  292. * 0: no writers, no VM_DENYWRITE mappings
  293. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  294. * > 0: (i_writecount) users are writing to the file.
  295. *
  296. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  297. * except for the cases where we don't hold i_writecount yet. Then we need to
  298. * use {get,deny}_write_access() - these functions check the sign and refuse
  299. * to do the change if sign is wrong. Exclusion between them is provided by
  300. * the inode->i_lock spinlock.
  301. */
  302. int get_write_access(struct inode * inode)
  303. {
  304. spin_lock(&inode->i_lock);
  305. if (atomic_read(&inode->i_writecount) < 0) {
  306. spin_unlock(&inode->i_lock);
  307. return -ETXTBSY;
  308. }
  309. atomic_inc(&inode->i_writecount);
  310. spin_unlock(&inode->i_lock);
  311. return 0;
  312. }
  313. int deny_write_access(struct file * file)
  314. {
  315. struct inode *inode = file->f_path.dentry->d_inode;
  316. spin_lock(&inode->i_lock);
  317. if (atomic_read(&inode->i_writecount) > 0) {
  318. spin_unlock(&inode->i_lock);
  319. return -ETXTBSY;
  320. }
  321. atomic_dec(&inode->i_writecount);
  322. spin_unlock(&inode->i_lock);
  323. return 0;
  324. }
  325. /**
  326. * path_get - get a reference to a path
  327. * @path: path to get the reference to
  328. *
  329. * Given a path increment the reference count to the dentry and the vfsmount.
  330. */
  331. void path_get(struct path *path)
  332. {
  333. mntget(path->mnt);
  334. dget(path->dentry);
  335. }
  336. EXPORT_SYMBOL(path_get);
  337. /**
  338. * path_put - put a reference to a path
  339. * @path: path to put the reference to
  340. *
  341. * Given a path decrement the reference count to the dentry and the vfsmount.
  342. */
  343. void path_put(struct path *path)
  344. {
  345. dput(path->dentry);
  346. mntput(path->mnt);
  347. }
  348. EXPORT_SYMBOL(path_put);
  349. /*
  350. * Path walking has 2 modes, rcu-walk and ref-walk (see
  351. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  352. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  353. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  354. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  355. * got stuck, so ref-walk may continue from there. If this is not successful
  356. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  357. * to restart the path walk from the beginning in ref-walk mode.
  358. */
  359. /**
  360. * unlazy_walk - try to switch to ref-walk mode.
  361. * @nd: nameidata pathwalk data
  362. * @dentry: child of nd->path.dentry or NULL
  363. * Returns: 0 on success, -ECHILD on failure
  364. *
  365. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  366. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  367. * @nd or NULL. Must be called from rcu-walk context.
  368. */
  369. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  370. {
  371. struct fs_struct *fs = current->fs;
  372. struct dentry *parent = nd->path.dentry;
  373. int want_root = 0;
  374. BUG_ON(!(nd->flags & LOOKUP_RCU));
  375. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  376. want_root = 1;
  377. spin_lock(&fs->lock);
  378. if (nd->root.mnt != fs->root.mnt ||
  379. nd->root.dentry != fs->root.dentry)
  380. goto err_root;
  381. }
  382. spin_lock(&parent->d_lock);
  383. if (!dentry) {
  384. if (!__d_rcu_to_refcount(parent, nd->seq))
  385. goto err_parent;
  386. BUG_ON(nd->inode != parent->d_inode);
  387. } else {
  388. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  389. if (!__d_rcu_to_refcount(dentry, nd->seq))
  390. goto err_child;
  391. /*
  392. * If the sequence check on the child dentry passed, then
  393. * the child has not been removed from its parent. This
  394. * means the parent dentry must be valid and able to take
  395. * a reference at this point.
  396. */
  397. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  398. BUG_ON(!parent->d_count);
  399. parent->d_count++;
  400. spin_unlock(&dentry->d_lock);
  401. }
  402. spin_unlock(&parent->d_lock);
  403. if (want_root) {
  404. path_get(&nd->root);
  405. spin_unlock(&fs->lock);
  406. }
  407. mntget(nd->path.mnt);
  408. rcu_read_unlock();
  409. br_read_unlock(vfsmount_lock);
  410. nd->flags &= ~LOOKUP_RCU;
  411. return 0;
  412. err_child:
  413. spin_unlock(&dentry->d_lock);
  414. err_parent:
  415. spin_unlock(&parent->d_lock);
  416. err_root:
  417. if (want_root)
  418. spin_unlock(&fs->lock);
  419. return -ECHILD;
  420. }
  421. /**
  422. * release_open_intent - free up open intent resources
  423. * @nd: pointer to nameidata
  424. */
  425. void release_open_intent(struct nameidata *nd)
  426. {
  427. struct file *file = nd->intent.open.file;
  428. if (file && !IS_ERR(file)) {
  429. if (file->f_path.dentry == NULL)
  430. put_filp(file);
  431. else
  432. fput(file);
  433. }
  434. }
  435. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  436. {
  437. return dentry->d_op->d_revalidate(dentry, nd);
  438. }
  439. static struct dentry *
  440. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  441. {
  442. int status = d_revalidate(dentry, nd);
  443. if (unlikely(status <= 0)) {
  444. /*
  445. * The dentry failed validation.
  446. * If d_revalidate returned 0 attempt to invalidate
  447. * the dentry otherwise d_revalidate is asking us
  448. * to return a fail status.
  449. */
  450. if (status < 0) {
  451. dput(dentry);
  452. dentry = ERR_PTR(status);
  453. } else if (!d_invalidate(dentry)) {
  454. dput(dentry);
  455. dentry = NULL;
  456. }
  457. }
  458. return dentry;
  459. }
  460. /**
  461. * complete_walk - successful completion of path walk
  462. * @nd: pointer nameidata
  463. *
  464. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  465. * Revalidate the final result, unless we'd already done that during
  466. * the path walk or the filesystem doesn't ask for it. Return 0 on
  467. * success, -error on failure. In case of failure caller does not
  468. * need to drop nd->path.
  469. */
  470. static int complete_walk(struct nameidata *nd)
  471. {
  472. struct dentry *dentry = nd->path.dentry;
  473. int status;
  474. if (nd->flags & LOOKUP_RCU) {
  475. nd->flags &= ~LOOKUP_RCU;
  476. if (!(nd->flags & LOOKUP_ROOT))
  477. nd->root.mnt = NULL;
  478. spin_lock(&dentry->d_lock);
  479. if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
  480. spin_unlock(&dentry->d_lock);
  481. rcu_read_unlock();
  482. br_read_unlock(vfsmount_lock);
  483. return -ECHILD;
  484. }
  485. BUG_ON(nd->inode != dentry->d_inode);
  486. spin_unlock(&dentry->d_lock);
  487. mntget(nd->path.mnt);
  488. rcu_read_unlock();
  489. br_read_unlock(vfsmount_lock);
  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);
  499. if (status > 0)
  500. return 0;
  501. if (!status)
  502. status = -ESTALE;
  503. path_put(&nd->path);
  504. return status;
  505. }
  506. /*
  507. * Short-cut version of permission(), for calling on directories
  508. * during pathname resolution. Combines parts of permission()
  509. * and generic_permission(), and tests ONLY for MAY_EXEC permission.
  510. *
  511. * If appropriate, check DAC only. If not appropriate, or
  512. * short-cut DAC fails, then call ->permission() to do more
  513. * complete permission check.
  514. */
  515. static inline int exec_permission(struct inode *inode, unsigned int flags)
  516. {
  517. int ret;
  518. struct user_namespace *ns = inode_userns(inode);
  519. if (inode->i_op->permission) {
  520. ret = inode->i_op->permission(inode, MAY_EXEC, flags);
  521. } else {
  522. ret = acl_permission_check(inode, MAY_EXEC, flags,
  523. inode->i_op->check_acl);
  524. }
  525. if (likely(!ret))
  526. goto ok;
  527. if (ret == -ECHILD)
  528. return ret;
  529. if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
  530. ns_capable(ns, CAP_DAC_READ_SEARCH))
  531. goto ok;
  532. return ret;
  533. ok:
  534. return security_inode_exec_permission(inode, flags);
  535. }
  536. static __always_inline void set_root(struct nameidata *nd)
  537. {
  538. if (!nd->root.mnt)
  539. get_fs_root(current->fs, &nd->root);
  540. }
  541. static int link_path_walk(const char *, struct nameidata *);
  542. static __always_inline void set_root_rcu(struct nameidata *nd)
  543. {
  544. if (!nd->root.mnt) {
  545. struct fs_struct *fs = current->fs;
  546. unsigned seq;
  547. do {
  548. seq = read_seqcount_begin(&fs->seq);
  549. nd->root = fs->root;
  550. nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  551. } while (read_seqcount_retry(&fs->seq, seq));
  552. }
  553. }
  554. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  555. {
  556. int ret;
  557. if (IS_ERR(link))
  558. goto fail;
  559. if (*link == '/') {
  560. set_root(nd);
  561. path_put(&nd->path);
  562. nd->path = nd->root;
  563. path_get(&nd->root);
  564. nd->flags |= LOOKUP_JUMPED;
  565. }
  566. nd->inode = nd->path.dentry->d_inode;
  567. ret = link_path_walk(link, nd);
  568. return ret;
  569. fail:
  570. path_put(&nd->path);
  571. return PTR_ERR(link);
  572. }
  573. static void path_put_conditional(struct path *path, struct nameidata *nd)
  574. {
  575. dput(path->dentry);
  576. if (path->mnt != nd->path.mnt)
  577. mntput(path->mnt);
  578. }
  579. static inline void path_to_nameidata(const struct path *path,
  580. struct nameidata *nd)
  581. {
  582. if (!(nd->flags & LOOKUP_RCU)) {
  583. dput(nd->path.dentry);
  584. if (nd->path.mnt != path->mnt)
  585. mntput(nd->path.mnt);
  586. }
  587. nd->path.mnt = path->mnt;
  588. nd->path.dentry = path->dentry;
  589. }
  590. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  591. {
  592. struct inode *inode = link->dentry->d_inode;
  593. if (!IS_ERR(cookie) && inode->i_op->put_link)
  594. inode->i_op->put_link(link->dentry, nd, cookie);
  595. path_put(link);
  596. }
  597. static __always_inline int
  598. follow_link(struct path *link, struct nameidata *nd, void **p)
  599. {
  600. int error;
  601. struct dentry *dentry = link->dentry;
  602. BUG_ON(nd->flags & LOOKUP_RCU);
  603. if (link->mnt == nd->path.mnt)
  604. mntget(link->mnt);
  605. if (unlikely(current->total_link_count >= 40)) {
  606. *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
  607. path_put(&nd->path);
  608. return -ELOOP;
  609. }
  610. cond_resched();
  611. current->total_link_count++;
  612. touch_atime(link->mnt, dentry);
  613. nd_set_link(nd, NULL);
  614. error = security_inode_follow_link(link->dentry, nd);
  615. if (error) {
  616. *p = ERR_PTR(error); /* no ->put_link(), please */
  617. path_put(&nd->path);
  618. return error;
  619. }
  620. nd->last_type = LAST_BIND;
  621. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  622. error = PTR_ERR(*p);
  623. if (!IS_ERR(*p)) {
  624. char *s = nd_get_link(nd);
  625. error = 0;
  626. if (s)
  627. error = __vfs_follow_link(nd, s);
  628. else if (nd->last_type == LAST_BIND) {
  629. nd->flags |= LOOKUP_JUMPED;
  630. nd->inode = nd->path.dentry->d_inode;
  631. if (nd->inode->i_op->follow_link) {
  632. /* stepped on a _really_ weird one */
  633. path_put(&nd->path);
  634. error = -ELOOP;
  635. }
  636. }
  637. }
  638. return error;
  639. }
  640. static int follow_up_rcu(struct path *path)
  641. {
  642. struct vfsmount *parent;
  643. struct dentry *mountpoint;
  644. parent = path->mnt->mnt_parent;
  645. if (parent == path->mnt)
  646. return 0;
  647. mountpoint = path->mnt->mnt_mountpoint;
  648. path->dentry = mountpoint;
  649. path->mnt = parent;
  650. return 1;
  651. }
  652. int follow_up(struct path *path)
  653. {
  654. struct vfsmount *parent;
  655. struct dentry *mountpoint;
  656. br_read_lock(vfsmount_lock);
  657. parent = path->mnt->mnt_parent;
  658. if (parent == path->mnt) {
  659. br_read_unlock(vfsmount_lock);
  660. return 0;
  661. }
  662. mntget(parent);
  663. mountpoint = dget(path->mnt->mnt_mountpoint);
  664. br_read_unlock(vfsmount_lock);
  665. dput(path->dentry);
  666. path->dentry = mountpoint;
  667. mntput(path->mnt);
  668. path->mnt = parent;
  669. return 1;
  670. }
  671. /*
  672. * Perform an automount
  673. * - return -EISDIR to tell follow_managed() to stop and return the path we
  674. * were called with.
  675. */
  676. static int follow_automount(struct path *path, unsigned flags,
  677. bool *need_mntput)
  678. {
  679. struct vfsmount *mnt;
  680. int err;
  681. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  682. return -EREMOTE;
  683. /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
  684. * and this is the terminal part of the path.
  685. */
  686. if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
  687. return -EISDIR; /* we actually want to stop here */
  688. /* We want to mount if someone is trying to open/create a file of any
  689. * type under the mountpoint, wants to traverse through the mountpoint
  690. * or wants to open the mounted directory.
  691. *
  692. * We don't want to mount if someone's just doing a stat and they've
  693. * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
  694. * appended a '/' to the name.
  695. */
  696. if (!(flags & LOOKUP_FOLLOW) &&
  697. !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
  698. LOOKUP_OPEN | LOOKUP_CREATE)))
  699. return -EISDIR;
  700. current->total_link_count++;
  701. if (current->total_link_count >= 40)
  702. return -ELOOP;
  703. mnt = path->dentry->d_op->d_automount(path);
  704. if (IS_ERR(mnt)) {
  705. /*
  706. * The filesystem is allowed to return -EISDIR here to indicate
  707. * it doesn't want to automount. For instance, autofs would do
  708. * this so that its userspace daemon can mount on this dentry.
  709. *
  710. * However, we can only permit this if it's a terminal point in
  711. * the path being looked up; if it wasn't then the remainder of
  712. * the path is inaccessible and we should say so.
  713. */
  714. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
  715. return -EREMOTE;
  716. return PTR_ERR(mnt);
  717. }
  718. if (!mnt) /* mount collision */
  719. return 0;
  720. err = finish_automount(mnt, path);
  721. switch (err) {
  722. case -EBUSY:
  723. /* Someone else made a mount here whilst we were busy */
  724. return 0;
  725. case 0:
  726. dput(path->dentry);
  727. if (*need_mntput)
  728. mntput(path->mnt);
  729. path->mnt = mnt;
  730. path->dentry = dget(mnt->mnt_root);
  731. *need_mntput = true;
  732. return 0;
  733. default:
  734. return err;
  735. }
  736. }
  737. /*
  738. * Handle a dentry that is managed in some way.
  739. * - Flagged for transit management (autofs)
  740. * - Flagged as mountpoint
  741. * - Flagged as automount point
  742. *
  743. * This may only be called in refwalk mode.
  744. *
  745. * Serialization is taken care of in namespace.c
  746. */
  747. static int follow_managed(struct path *path, unsigned flags)
  748. {
  749. unsigned managed;
  750. bool need_mntput = false;
  751. int ret;
  752. /* Given that we're not holding a lock here, we retain the value in a
  753. * local variable for each dentry as we look at it so that we don't see
  754. * the components of that value change under us */
  755. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  756. managed &= DCACHE_MANAGED_DENTRY,
  757. unlikely(managed != 0)) {
  758. /* Allow the filesystem to manage the transit without i_mutex
  759. * being held. */
  760. if (managed & DCACHE_MANAGE_TRANSIT) {
  761. BUG_ON(!path->dentry->d_op);
  762. BUG_ON(!path->dentry->d_op->d_manage);
  763. ret = path->dentry->d_op->d_manage(path->dentry, false);
  764. if (ret < 0)
  765. return ret == -EISDIR ? 0 : ret;
  766. }
  767. /* Transit to a mounted filesystem. */
  768. if (managed & DCACHE_MOUNTED) {
  769. struct vfsmount *mounted = lookup_mnt(path);
  770. if (mounted) {
  771. dput(path->dentry);
  772. if (need_mntput)
  773. mntput(path->mnt);
  774. path->mnt = mounted;
  775. path->dentry = dget(mounted->mnt_root);
  776. need_mntput = true;
  777. continue;
  778. }
  779. /* Something is mounted on this dentry in another
  780. * namespace and/or whatever was mounted there in this
  781. * namespace got unmounted before we managed to get the
  782. * vfsmount_lock */
  783. }
  784. /* Handle an automount point */
  785. if (managed & DCACHE_NEED_AUTOMOUNT) {
  786. ret = follow_automount(path, flags, &need_mntput);
  787. if (ret < 0)
  788. return ret == -EISDIR ? 0 : ret;
  789. continue;
  790. }
  791. /* We didn't change the current path point */
  792. break;
  793. }
  794. return 0;
  795. }
  796. int follow_down_one(struct path *path)
  797. {
  798. struct vfsmount *mounted;
  799. mounted = lookup_mnt(path);
  800. if (mounted) {
  801. dput(path->dentry);
  802. mntput(path->mnt);
  803. path->mnt = mounted;
  804. path->dentry = dget(mounted->mnt_root);
  805. return 1;
  806. }
  807. return 0;
  808. }
  809. static inline bool managed_dentry_might_block(struct dentry *dentry)
  810. {
  811. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  812. dentry->d_op->d_manage(dentry, true) < 0);
  813. }
  814. /*
  815. * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
  816. * meet a managed dentry and we're not walking to "..". True is returned to
  817. * continue, false to abort.
  818. */
  819. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  820. struct inode **inode, bool reverse_transit)
  821. {
  822. for (;;) {
  823. struct vfsmount *mounted;
  824. /*
  825. * Don't forget we might have a non-mountpoint managed dentry
  826. * that wants to block transit.
  827. */
  828. *inode = path->dentry->d_inode;
  829. if (!reverse_transit &&
  830. unlikely(managed_dentry_might_block(path->dentry)))
  831. return false;
  832. if (!d_mountpoint(path->dentry))
  833. break;
  834. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  835. if (!mounted)
  836. break;
  837. path->mnt = mounted;
  838. path->dentry = mounted->mnt_root;
  839. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  840. }
  841. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  842. return reverse_transit;
  843. return true;
  844. }
  845. static int follow_dotdot_rcu(struct nameidata *nd)
  846. {
  847. struct inode *inode = nd->inode;
  848. set_root_rcu(nd);
  849. while (1) {
  850. if (nd->path.dentry == nd->root.dentry &&
  851. nd->path.mnt == nd->root.mnt) {
  852. break;
  853. }
  854. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  855. struct dentry *old = nd->path.dentry;
  856. struct dentry *parent = old->d_parent;
  857. unsigned seq;
  858. seq = read_seqcount_begin(&parent->d_seq);
  859. if (read_seqcount_retry(&old->d_seq, nd->seq))
  860. goto failed;
  861. inode = parent->d_inode;
  862. nd->path.dentry = parent;
  863. nd->seq = seq;
  864. break;
  865. }
  866. if (!follow_up_rcu(&nd->path))
  867. break;
  868. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  869. inode = nd->path.dentry->d_inode;
  870. }
  871. __follow_mount_rcu(nd, &nd->path, &inode, true);
  872. nd->inode = inode;
  873. return 0;
  874. failed:
  875. nd->flags &= ~LOOKUP_RCU;
  876. if (!(nd->flags & LOOKUP_ROOT))
  877. nd->root.mnt = NULL;
  878. rcu_read_unlock();
  879. br_read_unlock(vfsmount_lock);
  880. return -ECHILD;
  881. }
  882. /*
  883. * Follow down to the covering mount currently visible to userspace. At each
  884. * point, the filesystem owning that dentry may be queried as to whether the
  885. * caller is permitted to proceed or not.
  886. *
  887. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  888. * being true).
  889. */
  890. int follow_down(struct path *path)
  891. {
  892. unsigned managed;
  893. int ret;
  894. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  895. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  896. /* Allow the filesystem to manage the transit without i_mutex
  897. * being held.
  898. *
  899. * We indicate to the filesystem if someone is trying to mount
  900. * something here. This gives autofs the chance to deny anyone
  901. * other than its daemon the right to mount on its
  902. * superstructure.
  903. *
  904. * The filesystem may sleep at this point.
  905. */
  906. if (managed & DCACHE_MANAGE_TRANSIT) {
  907. BUG_ON(!path->dentry->d_op);
  908. BUG_ON(!path->dentry->d_op->d_manage);
  909. ret = path->dentry->d_op->d_manage(
  910. path->dentry, false);
  911. if (ret < 0)
  912. return ret == -EISDIR ? 0 : ret;
  913. }
  914. /* Transit to a mounted filesystem. */
  915. if (managed & DCACHE_MOUNTED) {
  916. struct vfsmount *mounted = lookup_mnt(path);
  917. if (!mounted)
  918. break;
  919. dput(path->dentry);
  920. mntput(path->mnt);
  921. path->mnt = mounted;
  922. path->dentry = dget(mounted->mnt_root);
  923. continue;
  924. }
  925. /* Don't handle automount points here */
  926. break;
  927. }
  928. return 0;
  929. }
  930. /*
  931. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  932. */
  933. static void follow_mount(struct path *path)
  934. {
  935. while (d_mountpoint(path->dentry)) {
  936. struct vfsmount *mounted = lookup_mnt(path);
  937. if (!mounted)
  938. break;
  939. dput(path->dentry);
  940. mntput(path->mnt);
  941. path->mnt = mounted;
  942. path->dentry = dget(mounted->mnt_root);
  943. }
  944. }
  945. static void follow_dotdot(struct nameidata *nd)
  946. {
  947. set_root(nd);
  948. while(1) {
  949. struct dentry *old = nd->path.dentry;
  950. if (nd->path.dentry == nd->root.dentry &&
  951. nd->path.mnt == nd->root.mnt) {
  952. break;
  953. }
  954. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  955. /* rare case of legitimate dget_parent()... */
  956. nd->path.dentry = dget_parent(nd->path.dentry);
  957. dput(old);
  958. break;
  959. }
  960. if (!follow_up(&nd->path))
  961. break;
  962. }
  963. follow_mount(&nd->path);
  964. nd->inode = nd->path.dentry->d_inode;
  965. }
  966. /*
  967. * Allocate a dentry with name and parent, and perform a parent
  968. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  969. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  970. * have verified that no child exists while under i_mutex.
  971. */
  972. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  973. struct qstr *name, struct nameidata *nd)
  974. {
  975. struct inode *inode = parent->d_inode;
  976. struct dentry *dentry;
  977. struct dentry *old;
  978. /* Don't create child dentry for a dead directory. */
  979. if (unlikely(IS_DEADDIR(inode)))
  980. return ERR_PTR(-ENOENT);
  981. dentry = d_alloc(parent, name);
  982. if (unlikely(!dentry))
  983. return ERR_PTR(-ENOMEM);
  984. old = inode->i_op->lookup(inode, dentry, nd);
  985. if (unlikely(old)) {
  986. dput(dentry);
  987. dentry = old;
  988. }
  989. return dentry;
  990. }
  991. /*
  992. * It's more convoluted than I'd like it to be, but... it's still fairly
  993. * small and for now I'd prefer to have fast path as straight as possible.
  994. * It _is_ time-critical.
  995. */
  996. static int do_lookup(struct nameidata *nd, struct qstr *name,
  997. struct path *path, struct inode **inode)
  998. {
  999. struct vfsmount *mnt = nd->path.mnt;
  1000. struct dentry *dentry, *parent = nd->path.dentry;
  1001. int need_reval = 1;
  1002. int status = 1;
  1003. int err;
  1004. /*
  1005. * Rename seqlock is not required here because in the off chance
  1006. * of a false negative due to a concurrent rename, we're going to
  1007. * do the non-racy lookup, below.
  1008. */
  1009. if (nd->flags & LOOKUP_RCU) {
  1010. unsigned seq;
  1011. *inode = nd->inode;
  1012. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1013. if (!dentry)
  1014. goto unlazy;
  1015. /* Memory barrier in read_seqcount_begin of child is enough */
  1016. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1017. return -ECHILD;
  1018. nd->seq = seq;
  1019. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1020. status = d_revalidate(dentry, nd);
  1021. if (unlikely(status <= 0)) {
  1022. if (status != -ECHILD)
  1023. need_reval = 0;
  1024. goto unlazy;
  1025. }
  1026. }
  1027. path->mnt = mnt;
  1028. path->dentry = dentry;
  1029. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1030. return 0;
  1031. unlazy:
  1032. if (unlazy_walk(nd, dentry))
  1033. return -ECHILD;
  1034. } else {
  1035. dentry = __d_lookup(parent, name);
  1036. }
  1037. retry:
  1038. if (unlikely(!dentry)) {
  1039. struct inode *dir = parent->d_inode;
  1040. BUG_ON(nd->inode != dir);
  1041. mutex_lock(&dir->i_mutex);
  1042. dentry = d_lookup(parent, name);
  1043. if (likely(!dentry)) {
  1044. dentry = d_alloc_and_lookup(parent, name, nd);
  1045. if (IS_ERR(dentry)) {
  1046. mutex_unlock(&dir->i_mutex);
  1047. return PTR_ERR(dentry);
  1048. }
  1049. /* known good */
  1050. need_reval = 0;
  1051. status = 1;
  1052. }
  1053. mutex_unlock(&dir->i_mutex);
  1054. }
  1055. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1056. status = d_revalidate(dentry, nd);
  1057. if (unlikely(status <= 0)) {
  1058. if (status < 0) {
  1059. dput(dentry);
  1060. return status;
  1061. }
  1062. if (!d_invalidate(dentry)) {
  1063. dput(dentry);
  1064. dentry = NULL;
  1065. need_reval = 1;
  1066. goto retry;
  1067. }
  1068. }
  1069. path->mnt = mnt;
  1070. path->dentry = dentry;
  1071. err = follow_managed(path, nd->flags);
  1072. if (unlikely(err < 0)) {
  1073. path_put_conditional(path, nd);
  1074. return err;
  1075. }
  1076. *inode = path->dentry->d_inode;
  1077. return 0;
  1078. }
  1079. static inline int may_lookup(struct nameidata *nd)
  1080. {
  1081. if (nd->flags & LOOKUP_RCU) {
  1082. int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1083. if (err != -ECHILD)
  1084. return err;
  1085. if (unlazy_walk(nd, NULL))
  1086. return -ECHILD;
  1087. }
  1088. return exec_permission(nd->inode, 0);
  1089. }
  1090. static inline int handle_dots(struct nameidata *nd, int type)
  1091. {
  1092. if (type == LAST_DOTDOT) {
  1093. if (nd->flags & LOOKUP_RCU) {
  1094. if (follow_dotdot_rcu(nd))
  1095. return -ECHILD;
  1096. } else
  1097. follow_dotdot(nd);
  1098. }
  1099. return 0;
  1100. }
  1101. static void terminate_walk(struct nameidata *nd)
  1102. {
  1103. if (!(nd->flags & LOOKUP_RCU)) {
  1104. path_put(&nd->path);
  1105. } else {
  1106. nd->flags &= ~LOOKUP_RCU;
  1107. if (!(nd->flags & LOOKUP_ROOT))
  1108. nd->root.mnt = NULL;
  1109. rcu_read_unlock();
  1110. br_read_unlock(vfsmount_lock);
  1111. }
  1112. }
  1113. static inline int walk_component(struct nameidata *nd, struct path *path,
  1114. struct qstr *name, int type, int follow)
  1115. {
  1116. struct inode *inode;
  1117. int err;
  1118. /*
  1119. * "." and ".." are special - ".." especially so because it has
  1120. * to be able to know about the current root directory and
  1121. * parent relationships.
  1122. */
  1123. if (unlikely(type != LAST_NORM))
  1124. return handle_dots(nd, type);
  1125. err = do_lookup(nd, name, path, &inode);
  1126. if (unlikely(err)) {
  1127. terminate_walk(nd);
  1128. return err;
  1129. }
  1130. if (!inode) {
  1131. path_to_nameidata(path, nd);
  1132. terminate_walk(nd);
  1133. return -ENOENT;
  1134. }
  1135. if (unlikely(inode->i_op->follow_link) && follow) {
  1136. if (nd->flags & LOOKUP_RCU) {
  1137. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1138. terminate_walk(nd);
  1139. return -ECHILD;
  1140. }
  1141. }
  1142. BUG_ON(inode != path->dentry->d_inode);
  1143. return 1;
  1144. }
  1145. path_to_nameidata(path, nd);
  1146. nd->inode = inode;
  1147. return 0;
  1148. }
  1149. /*
  1150. * This limits recursive symlink follows to 8, while
  1151. * limiting consecutive symlinks to 40.
  1152. *
  1153. * Without that kind of total limit, nasty chains of consecutive
  1154. * symlinks can cause almost arbitrarily long lookups.
  1155. */
  1156. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1157. {
  1158. int res;
  1159. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1160. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1161. path_put_conditional(path, nd);
  1162. path_put(&nd->path);
  1163. return -ELOOP;
  1164. }
  1165. nd->depth++;
  1166. current->link_count++;
  1167. do {
  1168. struct path link = *path;
  1169. void *cookie;
  1170. res = follow_link(&link, nd, &cookie);
  1171. if (!res)
  1172. res = walk_component(nd, path, &nd->last,
  1173. nd->last_type, LOOKUP_FOLLOW);
  1174. put_link(nd, &link, cookie);
  1175. } while (res > 0);
  1176. current->link_count--;
  1177. nd->depth--;
  1178. return res;
  1179. }
  1180. /*
  1181. * Name resolution.
  1182. * This is the basic name resolution function, turning a pathname into
  1183. * the final dentry. We expect 'base' to be positive and a directory.
  1184. *
  1185. * Returns 0 and nd will have valid dentry and mnt on success.
  1186. * Returns error and drops reference to input namei data on failure.
  1187. */
  1188. static int link_path_walk(const char *name, struct nameidata *nd)
  1189. {
  1190. struct path next;
  1191. int err;
  1192. unsigned int lookup_flags = nd->flags;
  1193. while (*name=='/')
  1194. name++;
  1195. if (!*name)
  1196. return 0;
  1197. /* At this point we know we have a real path component. */
  1198. for(;;) {
  1199. unsigned long hash;
  1200. struct qstr this;
  1201. unsigned int c;
  1202. int type;
  1203. nd->flags |= LOOKUP_CONTINUE;
  1204. err = may_lookup(nd);
  1205. if (err)
  1206. break;
  1207. this.name = name;
  1208. c = *(const unsigned char *)name;
  1209. hash = init_name_hash();
  1210. do {
  1211. name++;
  1212. hash = partial_name_hash(c, hash);
  1213. c = *(const unsigned char *)name;
  1214. } while (c && (c != '/'));
  1215. this.len = name - (const char *) this.name;
  1216. this.hash = end_name_hash(hash);
  1217. type = LAST_NORM;
  1218. if (this.name[0] == '.') switch (this.len) {
  1219. case 2:
  1220. if (this.name[1] == '.') {
  1221. type = LAST_DOTDOT;
  1222. nd->flags |= LOOKUP_JUMPED;
  1223. }
  1224. break;
  1225. case 1:
  1226. type = LAST_DOT;
  1227. }
  1228. if (likely(type == LAST_NORM)) {
  1229. struct dentry *parent = nd->path.dentry;
  1230. nd->flags &= ~LOOKUP_JUMPED;
  1231. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1232. err = parent->d_op->d_hash(parent, nd->inode,
  1233. &this);
  1234. if (err < 0)
  1235. break;
  1236. }
  1237. }
  1238. /* remove trailing slashes? */
  1239. if (!c)
  1240. goto last_component;
  1241. while (*++name == '/');
  1242. if (!*name)
  1243. goto last_component;
  1244. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1245. if (err < 0)
  1246. return err;
  1247. if (err) {
  1248. err = nested_symlink(&next, nd);
  1249. if (err)
  1250. return err;
  1251. }
  1252. err = -ENOTDIR;
  1253. if (!nd->inode->i_op->lookup)
  1254. break;
  1255. continue;
  1256. /* here ends the main loop */
  1257. last_component:
  1258. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1259. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1260. nd->last = this;
  1261. nd->last_type = type;
  1262. return 0;
  1263. }
  1264. terminate_walk(nd);
  1265. return err;
  1266. }
  1267. static int path_init(int dfd, const char *name, unsigned int flags,
  1268. struct nameidata *nd, struct file **fp)
  1269. {
  1270. int retval = 0;
  1271. int fput_needed;
  1272. struct file *file;
  1273. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1274. nd->flags = flags | LOOKUP_JUMPED;
  1275. nd->depth = 0;
  1276. if (flags & LOOKUP_ROOT) {
  1277. struct inode *inode = nd->root.dentry->d_inode;
  1278. if (*name) {
  1279. if (!inode->i_op->lookup)
  1280. return -ENOTDIR;
  1281. retval = inode_permission(inode, MAY_EXEC);
  1282. if (retval)
  1283. return retval;
  1284. }
  1285. nd->path = nd->root;
  1286. nd->inode = inode;
  1287. if (flags & LOOKUP_RCU) {
  1288. br_read_lock(vfsmount_lock);
  1289. rcu_read_lock();
  1290. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1291. } else {
  1292. path_get(&nd->path);
  1293. }
  1294. return 0;
  1295. }
  1296. nd->root.mnt = NULL;
  1297. if (*name=='/') {
  1298. if (flags & LOOKUP_RCU) {
  1299. br_read_lock(vfsmount_lock);
  1300. rcu_read_lock();
  1301. set_root_rcu(nd);
  1302. } else {
  1303. set_root(nd);
  1304. path_get(&nd->root);
  1305. }
  1306. nd->path = nd->root;
  1307. } else if (dfd == AT_FDCWD) {
  1308. if (flags & LOOKUP_RCU) {
  1309. struct fs_struct *fs = current->fs;
  1310. unsigned seq;
  1311. br_read_lock(vfsmount_lock);
  1312. rcu_read_lock();
  1313. do {
  1314. seq = read_seqcount_begin(&fs->seq);
  1315. nd->path = fs->pwd;
  1316. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1317. } while (read_seqcount_retry(&fs->seq, seq));
  1318. } else {
  1319. get_fs_pwd(current->fs, &nd->path);
  1320. }
  1321. } else {
  1322. struct dentry *dentry;
  1323. file = fget_raw_light(dfd, &fput_needed);
  1324. retval = -EBADF;
  1325. if (!file)
  1326. goto out_fail;
  1327. dentry = file->f_path.dentry;
  1328. if (*name) {
  1329. retval = -ENOTDIR;
  1330. if (!S_ISDIR(dentry->d_inode->i_mode))
  1331. goto fput_fail;
  1332. retval = file_permission(file, MAY_EXEC);
  1333. if (retval)
  1334. goto fput_fail;
  1335. }
  1336. nd->path = file->f_path;
  1337. if (flags & LOOKUP_RCU) {
  1338. if (fput_needed)
  1339. *fp = file;
  1340. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1341. br_read_lock(vfsmount_lock);
  1342. rcu_read_lock();
  1343. } else {
  1344. path_get(&file->f_path);
  1345. fput_light(file, fput_needed);
  1346. }
  1347. }
  1348. nd->inode = nd->path.dentry->d_inode;
  1349. return 0;
  1350. fput_fail:
  1351. fput_light(file, fput_needed);
  1352. out_fail:
  1353. return retval;
  1354. }
  1355. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1356. {
  1357. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1358. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1359. nd->flags &= ~LOOKUP_PARENT;
  1360. return walk_component(nd, path, &nd->last, nd->last_type,
  1361. nd->flags & LOOKUP_FOLLOW);
  1362. }
  1363. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1364. static int path_lookupat(int dfd, const char *name,
  1365. unsigned int flags, struct nameidata *nd)
  1366. {
  1367. struct file *base = NULL;
  1368. struct path path;
  1369. int err;
  1370. /*
  1371. * Path walking is largely split up into 2 different synchronisation
  1372. * schemes, rcu-walk and ref-walk (explained in
  1373. * Documentation/filesystems/path-lookup.txt). These share much of the
  1374. * path walk code, but some things particularly setup, cleanup, and
  1375. * following mounts are sufficiently divergent that functions are
  1376. * duplicated. Typically there is a function foo(), and its RCU
  1377. * analogue, foo_rcu().
  1378. *
  1379. * -ECHILD is the error number of choice (just to avoid clashes) that
  1380. * is returned if some aspect of an rcu-walk fails. Such an error must
  1381. * be handled by restarting a traditional ref-walk (which will always
  1382. * be able to complete).
  1383. */
  1384. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1385. if (unlikely(err))
  1386. return err;
  1387. current->total_link_count = 0;
  1388. err = link_path_walk(name, nd);
  1389. if (!err && !(flags & LOOKUP_PARENT)) {
  1390. err = lookup_last(nd, &path);
  1391. while (err > 0) {
  1392. void *cookie;
  1393. struct path link = path;
  1394. nd->flags |= LOOKUP_PARENT;
  1395. err = follow_link(&link, nd, &cookie);
  1396. if (!err)
  1397. err = lookup_last(nd, &path);
  1398. put_link(nd, &link, cookie);
  1399. }
  1400. }
  1401. if (!err)
  1402. err = complete_walk(nd);
  1403. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1404. if (!nd->inode->i_op->lookup) {
  1405. path_put(&nd->path);
  1406. err = -ENOTDIR;
  1407. }
  1408. }
  1409. if (base)
  1410. fput(base);
  1411. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1412. path_put(&nd->root);
  1413. nd->root.mnt = NULL;
  1414. }
  1415. return err;
  1416. }
  1417. static int do_path_lookup(int dfd, const char *name,
  1418. unsigned int flags, struct nameidata *nd)
  1419. {
  1420. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1421. if (unlikely(retval == -ECHILD))
  1422. retval = path_lookupat(dfd, name, flags, nd);
  1423. if (unlikely(retval == -ESTALE))
  1424. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1425. if (likely(!retval)) {
  1426. if (unlikely(!audit_dummy_context())) {
  1427. if (nd->path.dentry && nd->inode)
  1428. audit_inode(name, nd->path.dentry);
  1429. }
  1430. }
  1431. return retval;
  1432. }
  1433. int kern_path_parent(const char *name, struct nameidata *nd)
  1434. {
  1435. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1436. }
  1437. int kern_path(const char *name, unsigned int flags, struct path *path)
  1438. {
  1439. struct nameidata nd;
  1440. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1441. if (!res)
  1442. *path = nd.path;
  1443. return res;
  1444. }
  1445. /**
  1446. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1447. * @dentry: pointer to dentry of the base directory
  1448. * @mnt: pointer to vfs mount of the base directory
  1449. * @name: pointer to file name
  1450. * @flags: lookup flags
  1451. * @nd: pointer to nameidata
  1452. */
  1453. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1454. const char *name, unsigned int flags,
  1455. struct nameidata *nd)
  1456. {
  1457. nd->root.dentry = dentry;
  1458. nd->root.mnt = mnt;
  1459. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1460. return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
  1461. }
  1462. static struct dentry *__lookup_hash(struct qstr *name,
  1463. struct dentry *base, struct nameidata *nd)
  1464. {
  1465. struct inode *inode = base->d_inode;
  1466. struct dentry *dentry;
  1467. int err;
  1468. err = exec_permission(inode, 0);
  1469. if (err)
  1470. return ERR_PTR(err);
  1471. /*
  1472. * Don't bother with __d_lookup: callers are for creat as
  1473. * well as unlink, so a lot of the time it would cost
  1474. * a double lookup.
  1475. */
  1476. dentry = d_lookup(base, name);
  1477. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1478. dentry = do_revalidate(dentry, nd);
  1479. if (!dentry)
  1480. dentry = d_alloc_and_lookup(base, name, nd);
  1481. return dentry;
  1482. }
  1483. /*
  1484. * Restricted form of lookup. Doesn't follow links, single-component only,
  1485. * needs parent already locked. Doesn't follow mounts.
  1486. * SMP-safe.
  1487. */
  1488. static struct dentry *lookup_hash(struct nameidata *nd)
  1489. {
  1490. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1491. }
  1492. /**
  1493. * lookup_one_len - filesystem helper to lookup single pathname component
  1494. * @name: pathname component to lookup
  1495. * @base: base directory to lookup from
  1496. * @len: maximum length @len should be interpreted to
  1497. *
  1498. * Note that this routine is purely a helper for filesystem usage and should
  1499. * not be called by generic code. Also note that by using this function the
  1500. * nameidata argument is passed to the filesystem methods and a filesystem
  1501. * using this helper needs to be prepared for that.
  1502. */
  1503. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1504. {
  1505. struct qstr this;
  1506. unsigned long hash;
  1507. unsigned int c;
  1508. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1509. this.name = name;
  1510. this.len = len;
  1511. if (!len)
  1512. return ERR_PTR(-EACCES);
  1513. hash = init_name_hash();
  1514. while (len--) {
  1515. c = *(const unsigned char *)name++;
  1516. if (c == '/' || c == '\0')
  1517. return ERR_PTR(-EACCES);
  1518. hash = partial_name_hash(c, hash);
  1519. }
  1520. this.hash = end_name_hash(hash);
  1521. /*
  1522. * See if the low-level filesystem might want
  1523. * to use its own hash..
  1524. */
  1525. if (base->d_flags & DCACHE_OP_HASH) {
  1526. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1527. if (err < 0)
  1528. return ERR_PTR(err);
  1529. }
  1530. return __lookup_hash(&this, base, NULL);
  1531. }
  1532. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1533. struct path *path)
  1534. {
  1535. struct nameidata nd;
  1536. char *tmp = getname_flags(name, flags);
  1537. int err = PTR_ERR(tmp);
  1538. if (!IS_ERR(tmp)) {
  1539. BUG_ON(flags & LOOKUP_PARENT);
  1540. err = do_path_lookup(dfd, tmp, flags, &nd);
  1541. putname(tmp);
  1542. if (!err)
  1543. *path = nd.path;
  1544. }
  1545. return err;
  1546. }
  1547. static int user_path_parent(int dfd, const char __user *path,
  1548. struct nameidata *nd, char **name)
  1549. {
  1550. char *s = getname(path);
  1551. int error;
  1552. if (IS_ERR(s))
  1553. return PTR_ERR(s);
  1554. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1555. if (error)
  1556. putname(s);
  1557. else
  1558. *name = s;
  1559. return error;
  1560. }
  1561. /*
  1562. * It's inline, so penalty for filesystems that don't use sticky bit is
  1563. * minimal.
  1564. */
  1565. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1566. {
  1567. uid_t fsuid = current_fsuid();
  1568. if (!(dir->i_mode & S_ISVTX))
  1569. return 0;
  1570. if (current_user_ns() != inode_userns(inode))
  1571. goto other_userns;
  1572. if (inode->i_uid == fsuid)
  1573. return 0;
  1574. if (dir->i_uid == fsuid)
  1575. return 0;
  1576. other_userns:
  1577. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1578. }
  1579. /*
  1580. * Check whether we can remove a link victim from directory dir, check
  1581. * whether the type of victim is right.
  1582. * 1. We can't do it if dir is read-only (done in permission())
  1583. * 2. We should have write and exec permissions on dir
  1584. * 3. We can't remove anything from append-only dir
  1585. * 4. We can't do anything with immutable dir (done in permission())
  1586. * 5. If the sticky bit on dir is set we should either
  1587. * a. be owner of dir, or
  1588. * b. be owner of victim, or
  1589. * c. have CAP_FOWNER capability
  1590. * 6. If the victim is append-only or immutable we can't do antyhing with
  1591. * links pointing to it.
  1592. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1593. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1594. * 9. We can't remove a root or mountpoint.
  1595. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1596. * nfs_async_unlink().
  1597. */
  1598. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1599. {
  1600. int error;
  1601. if (!victim->d_inode)
  1602. return -ENOENT;
  1603. BUG_ON(victim->d_parent->d_inode != dir);
  1604. audit_inode_child(victim, dir);
  1605. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1606. if (error)
  1607. return error;
  1608. if (IS_APPEND(dir))
  1609. return -EPERM;
  1610. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1611. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1612. return -EPERM;
  1613. if (isdir) {
  1614. if (!S_ISDIR(victim->d_inode->i_mode))
  1615. return -ENOTDIR;
  1616. if (IS_ROOT(victim))
  1617. return -EBUSY;
  1618. } else if (S_ISDIR(victim->d_inode->i_mode))
  1619. return -EISDIR;
  1620. if (IS_DEADDIR(dir))
  1621. return -ENOENT;
  1622. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1623. return -EBUSY;
  1624. return 0;
  1625. }
  1626. /* Check whether we can create an object with dentry child in directory
  1627. * dir.
  1628. * 1. We can't do it if child already exists (open has special treatment for
  1629. * this case, but since we are inlined it's OK)
  1630. * 2. We can't do it if dir is read-only (done in permission())
  1631. * 3. We should have write and exec permissions on dir
  1632. * 4. We can't do it if dir is immutable (done in permission())
  1633. */
  1634. static inline int may_create(struct inode *dir, struct dentry *child)
  1635. {
  1636. if (child->d_inode)
  1637. return -EEXIST;
  1638. if (IS_DEADDIR(dir))
  1639. return -ENOENT;
  1640. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1641. }
  1642. /*
  1643. * p1 and p2 should be directories on the same fs.
  1644. */
  1645. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1646. {
  1647. struct dentry *p;
  1648. if (p1 == p2) {
  1649. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1650. return NULL;
  1651. }
  1652. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1653. p = d_ancestor(p2, p1);
  1654. if (p) {
  1655. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1656. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1657. return p;
  1658. }
  1659. p = d_ancestor(p1, p2);
  1660. if (p) {
  1661. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1662. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1663. return p;
  1664. }
  1665. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1666. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1667. return NULL;
  1668. }
  1669. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1670. {
  1671. mutex_unlock(&p1->d_inode->i_mutex);
  1672. if (p1 != p2) {
  1673. mutex_unlock(&p2->d_inode->i_mutex);
  1674. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1675. }
  1676. }
  1677. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1678. struct nameidata *nd)
  1679. {
  1680. int error = may_create(dir, dentry);
  1681. if (error)
  1682. return error;
  1683. if (!dir->i_op->create)
  1684. return -EACCES; /* shouldn't it be ENOSYS? */
  1685. mode &= S_IALLUGO;
  1686. mode |= S_IFREG;
  1687. error = security_inode_create(dir, dentry, mode);
  1688. if (error)
  1689. return error;
  1690. error = dir->i_op->create(dir, dentry, mode, nd);
  1691. if (!error)
  1692. fsnotify_create(dir, dentry);
  1693. return error;
  1694. }
  1695. static int may_open(struct path *path, int acc_mode, int flag)
  1696. {
  1697. struct dentry *dentry = path->dentry;
  1698. struct inode *inode = dentry->d_inode;
  1699. int error;
  1700. /* O_PATH? */
  1701. if (!acc_mode)
  1702. return 0;
  1703. if (!inode)
  1704. return -ENOENT;
  1705. switch (inode->i_mode & S_IFMT) {
  1706. case S_IFLNK:
  1707. return -ELOOP;
  1708. case S_IFDIR:
  1709. if (acc_mode & MAY_WRITE)
  1710. return -EISDIR;
  1711. break;
  1712. case S_IFBLK:
  1713. case S_IFCHR:
  1714. if (path->mnt->mnt_flags & MNT_NODEV)
  1715. return -EACCES;
  1716. /*FALLTHRU*/
  1717. case S_IFIFO:
  1718. case S_IFSOCK:
  1719. flag &= ~O_TRUNC;
  1720. break;
  1721. }
  1722. error = inode_permission(inode, acc_mode);
  1723. if (error)
  1724. return error;
  1725. /*
  1726. * An append-only file must be opened in append mode for writing.
  1727. */
  1728. if (IS_APPEND(inode)) {
  1729. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1730. return -EPERM;
  1731. if (flag & O_TRUNC)
  1732. return -EPERM;
  1733. }
  1734. /* O_NOATIME can only be set by the owner or superuser */
  1735. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1736. return -EPERM;
  1737. /*
  1738. * Ensure there are no outstanding leases on the file.
  1739. */
  1740. return break_lease(inode, flag);
  1741. }
  1742. static int handle_truncate(struct file *filp)
  1743. {
  1744. struct path *path = &filp->f_path;
  1745. struct inode *inode = path->dentry->d_inode;
  1746. int error = get_write_access(inode);
  1747. if (error)
  1748. return error;
  1749. /*
  1750. * Refuse to truncate files with mandatory locks held on them.
  1751. */
  1752. error = locks_verify_locked(inode);
  1753. if (!error)
  1754. error = security_path_truncate(path);
  1755. if (!error) {
  1756. error = do_truncate(path->dentry, 0,
  1757. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1758. filp);
  1759. }
  1760. put_write_access(inode);
  1761. return error;
  1762. }
  1763. /*
  1764. * Note that while the flag value (low two bits) for sys_open means:
  1765. * 00 - read-only
  1766. * 01 - write-only
  1767. * 10 - read-write
  1768. * 11 - special
  1769. * it is changed into
  1770. * 00 - no permissions needed
  1771. * 01 - read-permission
  1772. * 10 - write-permission
  1773. * 11 - read-write
  1774. * for the internal routines (ie open_namei()/follow_link() etc)
  1775. * This is more logical, and also allows the 00 "no perm needed"
  1776. * to be used for symlinks (where the permissions are checked
  1777. * later).
  1778. *
  1779. */
  1780. static inline int open_to_namei_flags(int flag)
  1781. {
  1782. if ((flag+1) & O_ACCMODE)
  1783. flag++;
  1784. return flag;
  1785. }
  1786. /*
  1787. * Handle the last step of open()
  1788. */
  1789. static struct file *do_last(struct nameidata *nd, struct path *path,
  1790. const struct open_flags *op, const char *pathname)
  1791. {
  1792. struct dentry *dir = nd->path.dentry;
  1793. struct dentry *dentry;
  1794. int open_flag = op->open_flag;
  1795. int will_truncate = open_flag & O_TRUNC;
  1796. int want_write = 0;
  1797. int acc_mode = op->acc_mode;
  1798. struct file *filp;
  1799. int error;
  1800. nd->flags &= ~LOOKUP_PARENT;
  1801. nd->flags |= op->intent;
  1802. switch (nd->last_type) {
  1803. case LAST_DOTDOT:
  1804. case LAST_DOT:
  1805. error = handle_dots(nd, nd->last_type);
  1806. if (error)
  1807. return ERR_PTR(error);
  1808. /* fallthrough */
  1809. case LAST_ROOT:
  1810. error = complete_walk(nd);
  1811. if (error)
  1812. return ERR_PTR(error);
  1813. audit_inode(pathname, nd->path.dentry);
  1814. if (open_flag & O_CREAT) {
  1815. error = -EISDIR;
  1816. goto exit;
  1817. }
  1818. goto ok;
  1819. case LAST_BIND:
  1820. error = complete_walk(nd);
  1821. if (error)
  1822. return ERR_PTR(error);
  1823. audit_inode(pathname, dir);
  1824. goto ok;
  1825. }
  1826. if (!(open_flag & O_CREAT)) {
  1827. int symlink_ok = 0;
  1828. if (nd->last.name[nd->last.len])
  1829. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1830. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1831. symlink_ok = 1;
  1832. /* we _can_ be in RCU mode here */
  1833. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1834. !symlink_ok);
  1835. if (error < 0)
  1836. return ERR_PTR(error);
  1837. if (error) /* symlink */
  1838. return NULL;
  1839. /* sayonara */
  1840. error = complete_walk(nd);
  1841. if (error)
  1842. return ERR_PTR(-ECHILD);
  1843. error = -ENOTDIR;
  1844. if (nd->flags & LOOKUP_DIRECTORY) {
  1845. if (!nd->inode->i_op->lookup)
  1846. goto exit;
  1847. }
  1848. audit_inode(pathname, nd->path.dentry);
  1849. goto ok;
  1850. }
  1851. /* create side of things */
  1852. error = complete_walk(nd);
  1853. if (error)
  1854. return ERR_PTR(error);
  1855. audit_inode(pathname, dir);
  1856. error = -EISDIR;
  1857. /* trailing slashes? */
  1858. if (nd->last.name[nd->last.len])
  1859. goto exit;
  1860. mutex_lock(&dir->d_inode->i_mutex);
  1861. dentry = lookup_hash(nd);
  1862. error = PTR_ERR(dentry);
  1863. if (IS_ERR(dentry)) {
  1864. mutex_unlock(&dir->d_inode->i_mutex);
  1865. goto exit;
  1866. }
  1867. path->dentry = dentry;
  1868. path->mnt = nd->path.mnt;
  1869. /* Negative dentry, just create the file */
  1870. if (!dentry->d_inode) {
  1871. int mode = op->mode;
  1872. if (!IS_POSIXACL(dir->d_inode))
  1873. mode &= ~current_umask();
  1874. /*
  1875. * This write is needed to ensure that a
  1876. * rw->ro transition does not occur between
  1877. * the time when the file is created and when
  1878. * a permanent write count is taken through
  1879. * the 'struct file' in nameidata_to_filp().
  1880. */
  1881. error = mnt_want_write(nd->path.mnt);
  1882. if (error)
  1883. goto exit_mutex_unlock;
  1884. want_write = 1;
  1885. /* Don't check for write permission, don't truncate */
  1886. open_flag &= ~O_TRUNC;
  1887. will_truncate = 0;
  1888. acc_mode = MAY_OPEN;
  1889. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1890. if (error)
  1891. goto exit_mutex_unlock;
  1892. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1893. if (error)
  1894. goto exit_mutex_unlock;
  1895. mutex_unlock(&dir->d_inode->i_mutex);
  1896. dput(nd->path.dentry);
  1897. nd->path.dentry = dentry;
  1898. goto common;
  1899. }
  1900. /*
  1901. * It already exists.
  1902. */
  1903. mutex_unlock(&dir->d_inode->i_mutex);
  1904. audit_inode(pathname, path->dentry);
  1905. error = -EEXIST;
  1906. if (open_flag & O_EXCL)
  1907. goto exit_dput;
  1908. error = follow_managed(path, nd->flags);
  1909. if (error < 0)
  1910. goto exit_dput;
  1911. error = -ENOENT;
  1912. if (!path->dentry->d_inode)
  1913. goto exit_dput;
  1914. if (path->dentry->d_inode->i_op->follow_link)
  1915. return NULL;
  1916. path_to_nameidata(path, nd);
  1917. nd->inode = path->dentry->d_inode;
  1918. error = -EISDIR;
  1919. if (S_ISDIR(nd->inode->i_mode))
  1920. goto exit;
  1921. ok:
  1922. if (!S_ISREG(nd->inode->i_mode))
  1923. will_truncate = 0;
  1924. if (will_truncate) {
  1925. error = mnt_want_write(nd->path.mnt);
  1926. if (error)
  1927. goto exit;
  1928. want_write = 1;
  1929. }
  1930. common:
  1931. error = may_open(&nd->path, acc_mode, open_flag);
  1932. if (error)
  1933. goto exit;
  1934. filp = nameidata_to_filp(nd);
  1935. if (!IS_ERR(filp)) {
  1936. error = ima_file_check(filp, op->acc_mode);
  1937. if (error) {
  1938. fput(filp);
  1939. filp = ERR_PTR(error);
  1940. }
  1941. }
  1942. if (!IS_ERR(filp)) {
  1943. if (will_truncate) {
  1944. error = handle_truncate(filp);
  1945. if (error) {
  1946. fput(filp);
  1947. filp = ERR_PTR(error);
  1948. }
  1949. }
  1950. }
  1951. out:
  1952. if (want_write)
  1953. mnt_drop_write(nd->path.mnt);
  1954. path_put(&nd->path);
  1955. return filp;
  1956. exit_mutex_unlock:
  1957. mutex_unlock(&dir->d_inode->i_mutex);
  1958. exit_dput:
  1959. path_put_conditional(path, nd);
  1960. exit:
  1961. filp = ERR_PTR(error);
  1962. goto out;
  1963. }
  1964. static struct file *path_openat(int dfd, const char *pathname,
  1965. struct nameidata *nd, const struct open_flags *op, int flags)
  1966. {
  1967. struct file *base = NULL;
  1968. struct file *filp;
  1969. struct path path;
  1970. int error;
  1971. filp = get_empty_filp();
  1972. if (!filp)
  1973. return ERR_PTR(-ENFILE);
  1974. filp->f_flags = op->open_flag;
  1975. nd->intent.open.file = filp;
  1976. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  1977. nd->intent.open.create_mode = op->mode;
  1978. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  1979. if (unlikely(error))
  1980. goto out_filp;
  1981. current->total_link_count = 0;
  1982. error = link_path_walk(pathname, nd);
  1983. if (unlikely(error))
  1984. goto out_filp;
  1985. filp = do_last(nd, &path, op, pathname);
  1986. while (unlikely(!filp)) { /* trailing symlink */
  1987. struct path link = path;
  1988. void *cookie;
  1989. if (!(nd->flags & LOOKUP_FOLLOW)) {
  1990. path_put_conditional(&path, nd);
  1991. path_put(&nd->path);
  1992. filp = ERR_PTR(-ELOOP);
  1993. break;
  1994. }
  1995. nd->flags |= LOOKUP_PARENT;
  1996. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  1997. error = follow_link(&link, nd, &cookie);
  1998. if (unlikely(error))
  1999. filp = ERR_PTR(error);
  2000. else
  2001. filp = do_last(nd, &path, op, pathname);
  2002. put_link(nd, &link, cookie);
  2003. }
  2004. out:
  2005. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2006. path_put(&nd->root);
  2007. if (base)
  2008. fput(base);
  2009. release_open_intent(nd);
  2010. return filp;
  2011. out_filp:
  2012. filp = ERR_PTR(error);
  2013. goto out;
  2014. }
  2015. struct file *do_filp_open(int dfd, const char *pathname,
  2016. const struct open_flags *op, int flags)
  2017. {
  2018. struct nameidata nd;
  2019. struct file *filp;
  2020. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2021. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2022. filp = path_openat(dfd, pathname, &nd, op, flags);
  2023. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2024. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2025. return filp;
  2026. }
  2027. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2028. const char *name, const struct open_flags *op, int flags)
  2029. {
  2030. struct nameidata nd;
  2031. struct file *file;
  2032. nd.root.mnt = mnt;
  2033. nd.root.dentry = dentry;
  2034. flags |= LOOKUP_ROOT;
  2035. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2036. return ERR_PTR(-ELOOP);
  2037. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2038. if (unlikely(file == ERR_PTR(-ECHILD)))
  2039. file = path_openat(-1, name, &nd, op, flags);
  2040. if (unlikely(file == ERR_PTR(-ESTALE)))
  2041. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2042. return file;
  2043. }
  2044. /**
  2045. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2046. * @nd: nameidata info
  2047. * @is_dir: directory flag
  2048. *
  2049. * Simple function to lookup and return a dentry and create it
  2050. * if it doesn't exist. Is SMP-safe.
  2051. *
  2052. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2053. */
  2054. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2055. {
  2056. struct dentry *dentry = ERR_PTR(-EEXIST);
  2057. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2058. /*
  2059. * Yucky last component or no last component at all?
  2060. * (foo/., foo/.., /////)
  2061. */
  2062. if (nd->last_type != LAST_NORM)
  2063. goto fail;
  2064. nd->flags &= ~LOOKUP_PARENT;
  2065. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2066. nd->intent.open.flags = O_EXCL;
  2067. /*
  2068. * Do the final lookup.
  2069. */
  2070. dentry = lookup_hash(nd);
  2071. if (IS_ERR(dentry))
  2072. goto fail;
  2073. if (dentry->d_inode)
  2074. goto eexist;
  2075. /*
  2076. * Special case - lookup gave negative, but... we had foo/bar/
  2077. * From the vfs_mknod() POV we just have a negative dentry -
  2078. * all is fine. Let's be bastards - you had / on the end, you've
  2079. * been asking for (non-existent) directory. -ENOENT for you.
  2080. */
  2081. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2082. dput(dentry);
  2083. dentry = ERR_PTR(-ENOENT);
  2084. }
  2085. return dentry;
  2086. eexist:
  2087. dput(dentry);
  2088. dentry = ERR_PTR(-EEXIST);
  2089. fail:
  2090. return dentry;
  2091. }
  2092. EXPORT_SYMBOL_GPL(lookup_create);
  2093. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2094. {
  2095. int error = may_create(dir, dentry);
  2096. if (error)
  2097. return error;
  2098. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2099. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2100. return -EPERM;
  2101. if (!dir->i_op->mknod)
  2102. return -EPERM;
  2103. error = devcgroup_inode_mknod(mode, dev);
  2104. if (error)
  2105. return error;
  2106. error = security_inode_mknod(dir, dentry, mode, dev);
  2107. if (error)
  2108. return error;
  2109. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2110. if (!error)
  2111. fsnotify_create(dir, dentry);
  2112. return error;
  2113. }
  2114. static int may_mknod(mode_t mode)
  2115. {
  2116. switch (mode & S_IFMT) {
  2117. case S_IFREG:
  2118. case S_IFCHR:
  2119. case S_IFBLK:
  2120. case S_IFIFO:
  2121. case S_IFSOCK:
  2122. case 0: /* zero mode translates to S_IFREG */
  2123. return 0;
  2124. case S_IFDIR:
  2125. return -EPERM;
  2126. default:
  2127. return -EINVAL;
  2128. }
  2129. }
  2130. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2131. unsigned, dev)
  2132. {
  2133. int error;
  2134. char *tmp;
  2135. struct dentry *dentry;
  2136. struct nameidata nd;
  2137. if (S_ISDIR(mode))
  2138. return -EPERM;
  2139. error = user_path_parent(dfd, filename, &nd, &tmp);
  2140. if (error)
  2141. return error;
  2142. dentry = lookup_create(&nd, 0);
  2143. if (IS_ERR(dentry)) {
  2144. error = PTR_ERR(dentry);
  2145. goto out_unlock;
  2146. }
  2147. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2148. mode &= ~current_umask();
  2149. error = may_mknod(mode);
  2150. if (error)
  2151. goto out_dput;
  2152. error = mnt_want_write(nd.path.mnt);
  2153. if (error)
  2154. goto out_dput;
  2155. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2156. if (error)
  2157. goto out_drop_write;
  2158. switch (mode & S_IFMT) {
  2159. case 0: case S_IFREG:
  2160. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2161. break;
  2162. case S_IFCHR: case S_IFBLK:
  2163. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2164. new_decode_dev(dev));
  2165. break;
  2166. case S_IFIFO: case S_IFSOCK:
  2167. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2168. break;
  2169. }
  2170. out_drop_write:
  2171. mnt_drop_write(nd.path.mnt);
  2172. out_dput:
  2173. dput(dentry);
  2174. out_unlock:
  2175. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2176. path_put(&nd.path);
  2177. putname(tmp);
  2178. return error;
  2179. }
  2180. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2181. {
  2182. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2183. }
  2184. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2185. {
  2186. int error = may_create(dir, dentry);
  2187. if (error)
  2188. return error;
  2189. if (!dir->i_op->mkdir)
  2190. return -EPERM;
  2191. mode &= (S_IRWXUGO|S_ISVTX);
  2192. error = security_inode_mkdir(dir, dentry, mode);
  2193. if (error)
  2194. return error;
  2195. error = dir->i_op->mkdir(dir, dentry, mode);
  2196. if (!error)
  2197. fsnotify_mkdir(dir, dentry);
  2198. return error;
  2199. }
  2200. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2201. {
  2202. int error = 0;
  2203. char * tmp;
  2204. struct dentry *dentry;
  2205. struct nameidata nd;
  2206. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2207. if (error)
  2208. goto out_err;
  2209. dentry = lookup_create(&nd, 1);
  2210. error = PTR_ERR(dentry);
  2211. if (IS_ERR(dentry))
  2212. goto out_unlock;
  2213. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2214. mode &= ~current_umask();
  2215. error = mnt_want_write(nd.path.mnt);
  2216. if (error)
  2217. goto out_dput;
  2218. error = security_path_mkdir(&nd.path, dentry, mode);
  2219. if (error)
  2220. goto out_drop_write;
  2221. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2222. out_drop_write:
  2223. mnt_drop_write(nd.path.mnt);
  2224. out_dput:
  2225. dput(dentry);
  2226. out_unlock:
  2227. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2228. path_put(&nd.path);
  2229. putname(tmp);
  2230. out_err:
  2231. return error;
  2232. }
  2233. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2234. {
  2235. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2236. }
  2237. /*
  2238. * The dentry_unhash() helper will try to drop the dentry early: we
  2239. * should have a usage count of 2 if we're the only user of this
  2240. * dentry, and if that is true (possibly after pruning the dcache),
  2241. * then we drop the dentry now.
  2242. *
  2243. * A low-level filesystem can, if it choses, legally
  2244. * do a
  2245. *
  2246. * if (!d_unhashed(dentry))
  2247. * return -EBUSY;
  2248. *
  2249. * if it cannot handle the case of removing a directory
  2250. * that is still in use by something else..
  2251. */
  2252. void dentry_unhash(struct dentry *dentry)
  2253. {
  2254. shrink_dcache_parent(dentry);
  2255. spin_lock(&dentry->d_lock);
  2256. if (dentry->d_count == 1)
  2257. __d_drop(dentry);
  2258. spin_unlock(&dentry->d_lock);
  2259. }
  2260. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2261. {
  2262. int error = may_delete(dir, dentry, 1);
  2263. if (error)
  2264. return error;
  2265. if (!dir->i_op->rmdir)
  2266. return -EPERM;
  2267. mutex_lock(&dentry->d_inode->i_mutex);
  2268. error = -EBUSY;
  2269. if (d_mountpoint(dentry))
  2270. goto out;
  2271. error = security_inode_rmdir(dir, dentry);
  2272. if (error)
  2273. goto out;
  2274. error = dir->i_op->rmdir(dir, dentry);
  2275. if (error)
  2276. goto out;
  2277. dentry->d_inode->i_flags |= S_DEAD;
  2278. dont_mount(dentry);
  2279. out:
  2280. mutex_unlock(&dentry->d_inode->i_mutex);
  2281. if (!error)
  2282. d_delete(dentry);
  2283. return error;
  2284. }
  2285. static long do_rmdir(int dfd, const char __user *pathname)
  2286. {
  2287. int error = 0;
  2288. char * name;
  2289. struct dentry *dentry;
  2290. struct nameidata nd;
  2291. error = user_path_parent(dfd, pathname, &nd, &name);
  2292. if (error)
  2293. return error;
  2294. switch(nd.last_type) {
  2295. case LAST_DOTDOT:
  2296. error = -ENOTEMPTY;
  2297. goto exit1;
  2298. case LAST_DOT:
  2299. error = -EINVAL;
  2300. goto exit1;
  2301. case LAST_ROOT:
  2302. error = -EBUSY;
  2303. goto exit1;
  2304. }
  2305. nd.flags &= ~LOOKUP_PARENT;
  2306. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2307. dentry = lookup_hash(&nd);
  2308. error = PTR_ERR(dentry);
  2309. if (IS_ERR(dentry))
  2310. goto exit2;
  2311. error = mnt_want_write(nd.path.mnt);
  2312. if (error)
  2313. goto exit3;
  2314. error = security_path_rmdir(&nd.path, dentry);
  2315. if (error)
  2316. goto exit4;
  2317. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2318. exit4:
  2319. mnt_drop_write(nd.path.mnt);
  2320. exit3:
  2321. dput(dentry);
  2322. exit2:
  2323. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2324. exit1:
  2325. path_put(&nd.path);
  2326. putname(name);
  2327. return error;
  2328. }
  2329. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2330. {
  2331. return do_rmdir(AT_FDCWD, pathname);
  2332. }
  2333. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2334. {
  2335. int error = may_delete(dir, dentry, 0);
  2336. if (error)
  2337. return error;
  2338. if (!dir->i_op->unlink)
  2339. return -EPERM;
  2340. mutex_lock(&dentry->d_inode->i_mutex);
  2341. if (d_mountpoint(dentry))
  2342. error = -EBUSY;
  2343. else {
  2344. error = security_inode_unlink(dir, dentry);
  2345. if (!error) {
  2346. error = dir->i_op->unlink(dir, dentry);
  2347. if (!error)
  2348. dont_mount(dentry);
  2349. }
  2350. }
  2351. mutex_unlock(&dentry->d_inode->i_mutex);
  2352. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2353. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2354. fsnotify_link_count(dentry->d_inode);
  2355. d_delete(dentry);
  2356. }
  2357. return error;
  2358. }
  2359. /*
  2360. * Make sure that the actual truncation of the file will occur outside its
  2361. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2362. * writeout happening, and we don't want to prevent access to the directory
  2363. * while waiting on the I/O.
  2364. */
  2365. static long do_unlinkat(int dfd, const char __user *pathname)
  2366. {
  2367. int error;
  2368. char *name;
  2369. struct dentry *dentry;
  2370. struct nameidata nd;
  2371. struct inode *inode = NULL;
  2372. error = user_path_parent(dfd, pathname, &nd, &name);
  2373. if (error)
  2374. return error;
  2375. error = -EISDIR;
  2376. if (nd.last_type != LAST_NORM)
  2377. goto exit1;
  2378. nd.flags &= ~LOOKUP_PARENT;
  2379. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2380. dentry = lookup_hash(&nd);
  2381. error = PTR_ERR(dentry);
  2382. if (!IS_ERR(dentry)) {
  2383. /* Why not before? Because we want correct error value */
  2384. if (nd.last.name[nd.last.len])
  2385. goto slashes;
  2386. inode = dentry->d_inode;
  2387. if (inode)
  2388. ihold(inode);
  2389. error = mnt_want_write(nd.path.mnt);
  2390. if (error)
  2391. goto exit2;
  2392. error = security_path_unlink(&nd.path, dentry);
  2393. if (error)
  2394. goto exit3;
  2395. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2396. exit3:
  2397. mnt_drop_write(nd.path.mnt);
  2398. exit2:
  2399. dput(dentry);
  2400. }
  2401. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2402. if (inode)
  2403. iput(inode); /* truncate the inode here */
  2404. exit1:
  2405. path_put(&nd.path);
  2406. putname(name);
  2407. return error;
  2408. slashes:
  2409. error = !dentry->d_inode ? -ENOENT :
  2410. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2411. goto exit2;
  2412. }
  2413. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2414. {
  2415. if ((flag & ~AT_REMOVEDIR) != 0)
  2416. return -EINVAL;
  2417. if (flag & AT_REMOVEDIR)
  2418. return do_rmdir(dfd, pathname);
  2419. return do_unlinkat(dfd, pathname);
  2420. }
  2421. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2422. {
  2423. return do_unlinkat(AT_FDCWD, pathname);
  2424. }
  2425. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2426. {
  2427. int error = may_create(dir, dentry);
  2428. if (error)
  2429. return error;
  2430. if (!dir->i_op->symlink)
  2431. return -EPERM;
  2432. error = security_inode_symlink(dir, dentry, oldname);
  2433. if (error)
  2434. return error;
  2435. error = dir->i_op->symlink(dir, dentry, oldname);
  2436. if (!error)
  2437. fsnotify_create(dir, dentry);
  2438. return error;
  2439. }
  2440. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2441. int, newdfd, const char __user *, newname)
  2442. {
  2443. int error;
  2444. char *from;
  2445. char *to;
  2446. struct dentry *dentry;
  2447. struct nameidata nd;
  2448. from = getname(oldname);
  2449. if (IS_ERR(from))
  2450. return PTR_ERR(from);
  2451. error = user_path_parent(newdfd, newname, &nd, &to);
  2452. if (error)
  2453. goto out_putname;
  2454. dentry = lookup_create(&nd, 0);
  2455. error = PTR_ERR(dentry);
  2456. if (IS_ERR(dentry))
  2457. goto out_unlock;
  2458. error = mnt_want_write(nd.path.mnt);
  2459. if (error)
  2460. goto out_dput;
  2461. error = security_path_symlink(&nd.path, dentry, from);
  2462. if (error)
  2463. goto out_drop_write;
  2464. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2465. out_drop_write:
  2466. mnt_drop_write(nd.path.mnt);
  2467. out_dput:
  2468. dput(dentry);
  2469. out_unlock:
  2470. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2471. path_put(&nd.path);
  2472. putname(to);
  2473. out_putname:
  2474. putname(from);
  2475. return error;
  2476. }
  2477. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2478. {
  2479. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2480. }
  2481. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2482. {
  2483. struct inode *inode = old_dentry->d_inode;
  2484. int error;
  2485. if (!inode)
  2486. return -ENOENT;
  2487. error = may_create(dir, new_dentry);
  2488. if (error)
  2489. return error;
  2490. if (dir->i_sb != inode->i_sb)
  2491. return -EXDEV;
  2492. /*
  2493. * A link to an append-only or immutable file cannot be created.
  2494. */
  2495. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2496. return -EPERM;
  2497. if (!dir->i_op->link)
  2498. return -EPERM;
  2499. if (S_ISDIR(inode->i_mode))
  2500. return -EPERM;
  2501. error = security_inode_link(old_dentry, dir, new_dentry);
  2502. if (error)
  2503. return error;
  2504. mutex_lock(&inode->i_mutex);
  2505. /* Make sure we don't allow creating hardlink to an unlinked file */
  2506. if (inode->i_nlink == 0)
  2507. error = -ENOENT;
  2508. else
  2509. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2510. mutex_unlock(&inode->i_mutex);
  2511. if (!error)
  2512. fsnotify_link(dir, inode, new_dentry);
  2513. return error;
  2514. }
  2515. /*
  2516. * Hardlinks are often used in delicate situations. We avoid
  2517. * security-related surprises by not following symlinks on the
  2518. * newname. --KAB
  2519. *
  2520. * We don't follow them on the oldname either to be compatible
  2521. * with linux 2.0, and to avoid hard-linking to directories
  2522. * and other special files. --ADM
  2523. */
  2524. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2525. int, newdfd, const char __user *, newname, int, flags)
  2526. {
  2527. struct dentry *new_dentry;
  2528. struct nameidata nd;
  2529. struct path old_path;
  2530. int how = 0;
  2531. int error;
  2532. char *to;
  2533. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2534. return -EINVAL;
  2535. /*
  2536. * To use null names we require CAP_DAC_READ_SEARCH
  2537. * This ensures that not everyone will be able to create
  2538. * handlink using the passed filedescriptor.
  2539. */
  2540. if (flags & AT_EMPTY_PATH) {
  2541. if (!capable(CAP_DAC_READ_SEARCH))
  2542. return -ENOENT;
  2543. how = LOOKUP_EMPTY;
  2544. }
  2545. if (flags & AT_SYMLINK_FOLLOW)
  2546. how |= LOOKUP_FOLLOW;
  2547. error = user_path_at(olddfd, oldname, how, &old_path);
  2548. if (error)
  2549. return error;
  2550. error = user_path_parent(newdfd, newname, &nd, &to);
  2551. if (error)
  2552. goto out;
  2553. error = -EXDEV;
  2554. if (old_path.mnt != nd.path.mnt)
  2555. goto out_release;
  2556. new_dentry = lookup_create(&nd, 0);
  2557. error = PTR_ERR(new_dentry);
  2558. if (IS_ERR(new_dentry))
  2559. goto out_unlock;
  2560. error = mnt_want_write(nd.path.mnt);
  2561. if (error)
  2562. goto out_dput;
  2563. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2564. if (error)
  2565. goto out_drop_write;
  2566. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2567. out_drop_write:
  2568. mnt_drop_write(nd.path.mnt);
  2569. out_dput:
  2570. dput(new_dentry);
  2571. out_unlock:
  2572. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2573. out_release:
  2574. path_put(&nd.path);
  2575. putname(to);
  2576. out:
  2577. path_put(&old_path);
  2578. return error;
  2579. }
  2580. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2581. {
  2582. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2583. }
  2584. /*
  2585. * The worst of all namespace operations - renaming directory. "Perverted"
  2586. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2587. * Problems:
  2588. * a) we can get into loop creation. Check is done in is_subdir().
  2589. * b) race potential - two innocent renames can create a loop together.
  2590. * That's where 4.4 screws up. Current fix: serialization on
  2591. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2592. * story.
  2593. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2594. * And that - after we got ->i_mutex on parents (until then we don't know
  2595. * whether the target exists). Solution: try to be smart with locking
  2596. * order for inodes. We rely on the fact that tree topology may change
  2597. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2598. * move will be locked. Thus we can rank directories by the tree
  2599. * (ancestors first) and rank all non-directories after them.
  2600. * That works since everybody except rename does "lock parent, lookup,
  2601. * lock child" and rename is under ->s_vfs_rename_mutex.
  2602. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2603. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2604. * we'd better make sure that there's no link(2) for them.
  2605. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2606. * we are removing the target. Solution: we will have to grab ->i_mutex
  2607. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2608. * ->i_mutex on parents, which works but leads to some truly excessive
  2609. * locking].
  2610. */
  2611. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2612. struct inode *new_dir, struct dentry *new_dentry)
  2613. {
  2614. int error = 0;
  2615. struct inode *target = new_dentry->d_inode;
  2616. /*
  2617. * If we are going to change the parent - check write permissions,
  2618. * we'll need to flip '..'.
  2619. */
  2620. if (new_dir != old_dir) {
  2621. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2622. if (error)
  2623. return error;
  2624. }
  2625. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2626. if (error)
  2627. return error;
  2628. if (target)
  2629. mutex_lock(&target->i_mutex);
  2630. error = -EBUSY;
  2631. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2632. goto out;
  2633. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2634. if (error)
  2635. goto out;
  2636. if (target) {
  2637. target->i_flags |= S_DEAD;
  2638. dont_mount(new_dentry);
  2639. }
  2640. out:
  2641. if (target)
  2642. mutex_unlock(&target->i_mutex);
  2643. if (!error)
  2644. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2645. d_move(old_dentry,new_dentry);
  2646. return error;
  2647. }
  2648. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2649. struct inode *new_dir, struct dentry *new_dentry)
  2650. {
  2651. struct inode *target;
  2652. int error;
  2653. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2654. if (error)
  2655. return error;
  2656. dget(new_dentry);
  2657. target = new_dentry->d_inode;
  2658. if (target)
  2659. mutex_lock(&target->i_mutex);
  2660. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2661. error = -EBUSY;
  2662. else
  2663. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2664. if (!error) {
  2665. if (target)
  2666. dont_mount(new_dentry);
  2667. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2668. d_move(old_dentry, new_dentry);
  2669. }
  2670. if (target)
  2671. mutex_unlock(&target->i_mutex);
  2672. dput(new_dentry);
  2673. return error;
  2674. }
  2675. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2676. struct inode *new_dir, struct dentry *new_dentry)
  2677. {
  2678. int error;
  2679. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2680. const unsigned char *old_name;
  2681. if (old_dentry->d_inode == new_dentry->d_inode)
  2682. return 0;
  2683. error = may_delete(old_dir, old_dentry, is_dir);
  2684. if (error)
  2685. return error;
  2686. if (!new_dentry->d_inode)
  2687. error = may_create(new_dir, new_dentry);
  2688. else
  2689. error = may_delete(new_dir, new_dentry, is_dir);
  2690. if (error)
  2691. return error;
  2692. if (!old_dir->i_op->rename)
  2693. return -EPERM;
  2694. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2695. if (is_dir)
  2696. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2697. else
  2698. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2699. if (!error)
  2700. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2701. new_dentry->d_inode, old_dentry);
  2702. fsnotify_oldname_free(old_name);
  2703. return error;
  2704. }
  2705. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2706. int, newdfd, const char __user *, newname)
  2707. {
  2708. struct dentry *old_dir, *new_dir;
  2709. struct dentry *old_dentry, *new_dentry;
  2710. struct dentry *trap;
  2711. struct nameidata oldnd, newnd;
  2712. char *from;
  2713. char *to;
  2714. int error;
  2715. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2716. if (error)
  2717. goto exit;
  2718. error = user_path_parent(newdfd, newname, &newnd, &to);
  2719. if (error)
  2720. goto exit1;
  2721. error = -EXDEV;
  2722. if (oldnd.path.mnt != newnd.path.mnt)
  2723. goto exit2;
  2724. old_dir = oldnd.path.dentry;
  2725. error = -EBUSY;
  2726. if (oldnd.last_type != LAST_NORM)
  2727. goto exit2;
  2728. new_dir = newnd.path.dentry;
  2729. if (newnd.last_type != LAST_NORM)
  2730. goto exit2;
  2731. oldnd.flags &= ~LOOKUP_PARENT;
  2732. newnd.flags &= ~LOOKUP_PARENT;
  2733. newnd.flags |= LOOKUP_RENAME_TARGET;
  2734. trap = lock_rename(new_dir, old_dir);
  2735. old_dentry = lookup_hash(&oldnd);
  2736. error = PTR_ERR(old_dentry);
  2737. if (IS_ERR(old_dentry))
  2738. goto exit3;
  2739. /* source must exist */
  2740. error = -ENOENT;
  2741. if (!old_dentry->d_inode)
  2742. goto exit4;
  2743. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2744. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2745. error = -ENOTDIR;
  2746. if (oldnd.last.name[oldnd.last.len])
  2747. goto exit4;
  2748. if (newnd.last.name[newnd.last.len])
  2749. goto exit4;
  2750. }
  2751. /* source should not be ancestor of target */
  2752. error = -EINVAL;
  2753. if (old_dentry == trap)
  2754. goto exit4;
  2755. new_dentry = lookup_hash(&newnd);
  2756. error = PTR_ERR(new_dentry);
  2757. if (IS_ERR(new_dentry))
  2758. goto exit4;
  2759. /* target should not be an ancestor of source */
  2760. error = -ENOTEMPTY;
  2761. if (new_dentry == trap)
  2762. goto exit5;
  2763. error = mnt_want_write(oldnd.path.mnt);
  2764. if (error)
  2765. goto exit5;
  2766. error = security_path_rename(&oldnd.path, old_dentry,
  2767. &newnd.path, new_dentry);
  2768. if (error)
  2769. goto exit6;
  2770. error = vfs_rename(old_dir->d_inode, old_dentry,
  2771. new_dir->d_inode, new_dentry);
  2772. exit6:
  2773. mnt_drop_write(oldnd.path.mnt);
  2774. exit5:
  2775. dput(new_dentry);
  2776. exit4:
  2777. dput(old_dentry);
  2778. exit3:
  2779. unlock_rename(new_dir, old_dir);
  2780. exit2:
  2781. path_put(&newnd.path);
  2782. putname(to);
  2783. exit1:
  2784. path_put(&oldnd.path);
  2785. putname(from);
  2786. exit:
  2787. return error;
  2788. }
  2789. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2790. {
  2791. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2792. }
  2793. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2794. {
  2795. int len;
  2796. len = PTR_ERR(link);
  2797. if (IS_ERR(link))
  2798. goto out;
  2799. len = strlen(link);
  2800. if (len > (unsigned) buflen)
  2801. len = buflen;
  2802. if (copy_to_user(buffer, link, len))
  2803. len = -EFAULT;
  2804. out:
  2805. return len;
  2806. }
  2807. /*
  2808. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2809. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2810. * using) it for any given inode is up to filesystem.
  2811. */
  2812. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2813. {
  2814. struct nameidata nd;
  2815. void *cookie;
  2816. int res;
  2817. nd.depth = 0;
  2818. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2819. if (IS_ERR(cookie))
  2820. return PTR_ERR(cookie);
  2821. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2822. if (dentry->d_inode->i_op->put_link)
  2823. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2824. return res;
  2825. }
  2826. int vfs_follow_link(struct nameidata *nd, const char *link)
  2827. {
  2828. return __vfs_follow_link(nd, link);
  2829. }
  2830. /* get the link contents into pagecache */
  2831. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2832. {
  2833. char *kaddr;
  2834. struct page *page;
  2835. struct address_space *mapping = dentry->d_inode->i_mapping;
  2836. page = read_mapping_page(mapping, 0, NULL);
  2837. if (IS_ERR(page))
  2838. return (char*)page;
  2839. *ppage = page;
  2840. kaddr = kmap(page);
  2841. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2842. return kaddr;
  2843. }
  2844. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2845. {
  2846. struct page *page = NULL;
  2847. char *s = page_getlink(dentry, &page);
  2848. int res = vfs_readlink(dentry,buffer,buflen,s);
  2849. if (page) {
  2850. kunmap(page);
  2851. page_cache_release(page);
  2852. }
  2853. return res;
  2854. }
  2855. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2856. {
  2857. struct page *page = NULL;
  2858. nd_set_link(nd, page_getlink(dentry, &page));
  2859. return page;
  2860. }
  2861. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2862. {
  2863. struct page *page = cookie;
  2864. if (page) {
  2865. kunmap(page);
  2866. page_cache_release(page);
  2867. }
  2868. }
  2869. /*
  2870. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2871. */
  2872. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2873. {
  2874. struct address_space *mapping = inode->i_mapping;
  2875. struct page *page;
  2876. void *fsdata;
  2877. int err;
  2878. char *kaddr;
  2879. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2880. if (nofs)
  2881. flags |= AOP_FLAG_NOFS;
  2882. retry:
  2883. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2884. flags, &page, &fsdata);
  2885. if (err)
  2886. goto fail;
  2887. kaddr = kmap_atomic(page, KM_USER0);
  2888. memcpy(kaddr, symname, len-1);
  2889. kunmap_atomic(kaddr, KM_USER0);
  2890. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2891. page, fsdata);
  2892. if (err < 0)
  2893. goto fail;
  2894. if (err < len-1)
  2895. goto retry;
  2896. mark_inode_dirty(inode);
  2897. return 0;
  2898. fail:
  2899. return err;
  2900. }
  2901. int page_symlink(struct inode *inode, const char *symname, int len)
  2902. {
  2903. return __page_symlink(inode, symname, len,
  2904. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2905. }
  2906. const struct inode_operations page_symlink_inode_operations = {
  2907. .readlink = generic_readlink,
  2908. .follow_link = page_follow_link_light,
  2909. .put_link = page_put_link,
  2910. };
  2911. EXPORT_SYMBOL(user_path_at);
  2912. EXPORT_SYMBOL(follow_down_one);
  2913. EXPORT_SYMBOL(follow_down);
  2914. EXPORT_SYMBOL(follow_up);
  2915. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2916. EXPORT_SYMBOL(getname);
  2917. EXPORT_SYMBOL(lock_rename);
  2918. EXPORT_SYMBOL(lookup_one_len);
  2919. EXPORT_SYMBOL(page_follow_link_light);
  2920. EXPORT_SYMBOL(page_put_link);
  2921. EXPORT_SYMBOL(page_readlink);
  2922. EXPORT_SYMBOL(__page_symlink);
  2923. EXPORT_SYMBOL(page_symlink);
  2924. EXPORT_SYMBOL(page_symlink_inode_operations);
  2925. EXPORT_SYMBOL(kern_path_parent);
  2926. EXPORT_SYMBOL(kern_path);
  2927. EXPORT_SYMBOL(vfs_path_lookup);
  2928. EXPORT_SYMBOL(inode_permission);
  2929. EXPORT_SYMBOL(file_permission);
  2930. EXPORT_SYMBOL(unlock_rename);
  2931. EXPORT_SYMBOL(vfs_create);
  2932. EXPORT_SYMBOL(vfs_follow_link);
  2933. EXPORT_SYMBOL(vfs_link);
  2934. EXPORT_SYMBOL(vfs_mkdir);
  2935. EXPORT_SYMBOL(vfs_mknod);
  2936. EXPORT_SYMBOL(generic_permission);
  2937. EXPORT_SYMBOL(vfs_readlink);
  2938. EXPORT_SYMBOL(vfs_rename);
  2939. EXPORT_SYMBOL(vfs_rmdir);
  2940. EXPORT_SYMBOL(vfs_symlink);
  2941. EXPORT_SYMBOL(vfs_unlink);
  2942. EXPORT_SYMBOL(dentry_unhash);
  2943. EXPORT_SYMBOL(generic_readlink);