namei.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865
  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/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.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 <asm/uaccess.h>
  32. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  33. /* [Feb-1997 T. Schoebel-Theuer]
  34. * Fundamental changes in the pathname lookup mechanisms (namei)
  35. * were necessary because of omirr. The reason is that omirr needs
  36. * to know the _real_ pathname, not the user-supplied one, in case
  37. * of symlinks (and also when transname replacements occur).
  38. *
  39. * The new code replaces the old recursive symlink resolution with
  40. * an iterative one (in case of non-nested symlink chains). It does
  41. * this with calls to <fs>_follow_link().
  42. * As a side effect, dir_namei(), _namei() and follow_link() are now
  43. * replaced with a single function lookup_dentry() that can handle all
  44. * the special cases of the former code.
  45. *
  46. * With the new dcache, the pathname is stored at each inode, at least as
  47. * long as the refcount of the inode is positive. As a side effect, the
  48. * size of the dcache depends on the inode cache and thus is dynamic.
  49. *
  50. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  51. * resolution to correspond with current state of the code.
  52. *
  53. * Note that the symlink resolution is not *completely* iterative.
  54. * There is still a significant amount of tail- and mid- recursion in
  55. * the algorithm. Also, note that <fs>_readlink() is not used in
  56. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  57. * may return different results than <fs>_follow_link(). Many virtual
  58. * filesystems (including /proc) exhibit this behavior.
  59. */
  60. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  61. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  62. * and the name already exists in form of a symlink, try to create the new
  63. * name indicated by the symlink. The old code always complained that the
  64. * name already exists, due to not following the symlink even if its target
  65. * is nonexistent. The new semantics affects also mknod() and link() when
  66. * the name is a symlink pointing to a non-existant name.
  67. *
  68. * I don't know which semantics is the right one, since I have no access
  69. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  70. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  71. * "old" one. Personally, I think the new semantics is much more logical.
  72. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  73. * file does succeed in both HP-UX and SunOs, but not in Solaris
  74. * and in the old Linux semantics.
  75. */
  76. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  77. * semantics. See the comments in "open_namei" and "do_link" below.
  78. *
  79. * [10-Sep-98 Alan Modra] Another symlink change.
  80. */
  81. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  82. * inside the path - always follow.
  83. * in the last component in creation/removal/renaming - never follow.
  84. * if LOOKUP_FOLLOW passed - follow.
  85. * if the pathname has trailing slashes - follow.
  86. * otherwise - don't follow.
  87. * (applied in that order).
  88. *
  89. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  90. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  91. * During the 2.4 we need to fix the userland stuff depending on it -
  92. * hopefully we will be able to get rid of that wart in 2.5. So far only
  93. * XEmacs seems to be relying on it...
  94. */
  95. /*
  96. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  97. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  98. * any extra contention...
  99. */
  100. static int __link_path_walk(const char *name, struct nameidata *nd);
  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. char * getname(const char __user * filename)
  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. __putname(tmp);
  137. result = ERR_PTR(retval);
  138. }
  139. }
  140. audit_getname(result);
  141. return result;
  142. }
  143. #ifdef CONFIG_AUDITSYSCALL
  144. void putname(const char *name)
  145. {
  146. if (unlikely(!audit_dummy_context()))
  147. audit_putname(name);
  148. else
  149. __putname(name);
  150. }
  151. EXPORT_SYMBOL(putname);
  152. #endif
  153. /**
  154. * generic_permission - check for access rights on a Posix-like filesystem
  155. * @inode: inode to check access rights for
  156. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  157. * @check_acl: optional callback to check for Posix ACLs
  158. *
  159. * Used to check for read/write/execute permissions on a file.
  160. * We use "fsuid" for this, letting us set arbitrary permissions
  161. * for filesystem access without changing the "normal" uids which
  162. * are used for other things..
  163. */
  164. int generic_permission(struct inode *inode, int mask,
  165. int (*check_acl)(struct inode *inode, int mask))
  166. {
  167. umode_t mode = inode->i_mode;
  168. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  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);
  174. if (error == -EACCES)
  175. goto check_capabilities;
  176. else if (error != -EAGAIN)
  177. return error;
  178. }
  179. if (in_group_p(inode->i_gid))
  180. mode >>= 3;
  181. }
  182. /*
  183. * If the DACs are ok we don't need any capability check.
  184. */
  185. if ((mask & ~mode) == 0)
  186. return 0;
  187. check_capabilities:
  188. /*
  189. * Read/write DACs are always overridable.
  190. * Executable DACs are overridable if at least one exec bit is set.
  191. */
  192. if (!(mask & MAY_EXEC) || execute_ok(inode))
  193. if (capable(CAP_DAC_OVERRIDE))
  194. return 0;
  195. /*
  196. * Searching includes executable on directories, else just read.
  197. */
  198. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  199. if (capable(CAP_DAC_READ_SEARCH))
  200. return 0;
  201. return -EACCES;
  202. }
  203. int inode_permission(struct inode *inode, int mask)
  204. {
  205. int retval;
  206. if (mask & MAY_WRITE) {
  207. umode_t mode = inode->i_mode;
  208. /*
  209. * Nobody gets write access to a read-only fs.
  210. */
  211. if (IS_RDONLY(inode) &&
  212. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  213. return -EROFS;
  214. /*
  215. * Nobody gets write access to an immutable file.
  216. */
  217. if (IS_IMMUTABLE(inode))
  218. return -EACCES;
  219. }
  220. /* Ordinary permission routines do not understand MAY_APPEND. */
  221. if (inode->i_op && inode->i_op->permission)
  222. retval = inode->i_op->permission(inode, mask);
  223. else
  224. retval = generic_permission(inode, mask, NULL);
  225. if (retval)
  226. return retval;
  227. retval = devcgroup_inode_permission(inode, mask);
  228. if (retval)
  229. return retval;
  230. return security_inode_permission(inode,
  231. mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
  232. }
  233. /**
  234. * vfs_permission - check for access rights to a given path
  235. * @nd: lookup result that describes the path
  236. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  237. *
  238. * Used to check for read/write/execute permissions on a path.
  239. * We use "fsuid" for this, letting us set arbitrary permissions
  240. * for filesystem access without changing the "normal" uids which
  241. * are used for other things.
  242. */
  243. int vfs_permission(struct nameidata *nd, int mask)
  244. {
  245. return inode_permission(nd->path.dentry->d_inode, mask);
  246. }
  247. /**
  248. * file_permission - check for additional access rights to a given file
  249. * @file: file to check access rights for
  250. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  251. *
  252. * Used to check for read/write/execute permissions on an already opened
  253. * file.
  254. *
  255. * Note:
  256. * Do not use this function in new code. All access checks should
  257. * be done using vfs_permission().
  258. */
  259. int file_permission(struct file *file, int mask)
  260. {
  261. return inode_permission(file->f_path.dentry->d_inode, mask);
  262. }
  263. /*
  264. * get_write_access() gets write permission for a file.
  265. * put_write_access() releases this write permission.
  266. * This is used for regular files.
  267. * We cannot support write (and maybe mmap read-write shared) accesses and
  268. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  269. * can have the following values:
  270. * 0: no writers, no VM_DENYWRITE mappings
  271. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  272. * > 0: (i_writecount) users are writing to the file.
  273. *
  274. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  275. * except for the cases where we don't hold i_writecount yet. Then we need to
  276. * use {get,deny}_write_access() - these functions check the sign and refuse
  277. * to do the change if sign is wrong. Exclusion between them is provided by
  278. * the inode->i_lock spinlock.
  279. */
  280. int get_write_access(struct inode * inode)
  281. {
  282. spin_lock(&inode->i_lock);
  283. if (atomic_read(&inode->i_writecount) < 0) {
  284. spin_unlock(&inode->i_lock);
  285. return -ETXTBSY;
  286. }
  287. atomic_inc(&inode->i_writecount);
  288. spin_unlock(&inode->i_lock);
  289. return 0;
  290. }
  291. int deny_write_access(struct file * file)
  292. {
  293. struct inode *inode = file->f_path.dentry->d_inode;
  294. spin_lock(&inode->i_lock);
  295. if (atomic_read(&inode->i_writecount) > 0) {
  296. spin_unlock(&inode->i_lock);
  297. return -ETXTBSY;
  298. }
  299. atomic_dec(&inode->i_writecount);
  300. spin_unlock(&inode->i_lock);
  301. return 0;
  302. }
  303. /**
  304. * path_get - get a reference to a path
  305. * @path: path to get the reference to
  306. *
  307. * Given a path increment the reference count to the dentry and the vfsmount.
  308. */
  309. void path_get(struct path *path)
  310. {
  311. mntget(path->mnt);
  312. dget(path->dentry);
  313. }
  314. EXPORT_SYMBOL(path_get);
  315. /**
  316. * path_put - put a reference to a path
  317. * @path: path to put the reference to
  318. *
  319. * Given a path decrement the reference count to the dentry and the vfsmount.
  320. */
  321. void path_put(struct path *path)
  322. {
  323. dput(path->dentry);
  324. mntput(path->mnt);
  325. }
  326. EXPORT_SYMBOL(path_put);
  327. /**
  328. * release_open_intent - free up open intent resources
  329. * @nd: pointer to nameidata
  330. */
  331. void release_open_intent(struct nameidata *nd)
  332. {
  333. if (nd->intent.open.file->f_path.dentry == NULL)
  334. put_filp(nd->intent.open.file);
  335. else
  336. fput(nd->intent.open.file);
  337. }
  338. static inline struct dentry *
  339. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  340. {
  341. int status = dentry->d_op->d_revalidate(dentry, nd);
  342. if (unlikely(status <= 0)) {
  343. /*
  344. * The dentry failed validation.
  345. * If d_revalidate returned 0 attempt to invalidate
  346. * the dentry otherwise d_revalidate is asking us
  347. * to return a fail status.
  348. */
  349. if (!status) {
  350. if (!d_invalidate(dentry)) {
  351. dput(dentry);
  352. dentry = NULL;
  353. }
  354. } else {
  355. dput(dentry);
  356. dentry = ERR_PTR(status);
  357. }
  358. }
  359. return dentry;
  360. }
  361. /*
  362. * Internal lookup() using the new generic dcache.
  363. * SMP-safe
  364. */
  365. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  366. {
  367. struct dentry * dentry = __d_lookup(parent, name);
  368. /* lockess __d_lookup may fail due to concurrent d_move()
  369. * in some unrelated directory, so try with d_lookup
  370. */
  371. if (!dentry)
  372. dentry = d_lookup(parent, name);
  373. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  374. dentry = do_revalidate(dentry, nd);
  375. return dentry;
  376. }
  377. /*
  378. * Short-cut version of permission(), for calling by
  379. * path_walk(), when dcache lock is held. Combines parts
  380. * of permission() and generic_permission(), and tests ONLY for
  381. * MAY_EXEC permission.
  382. *
  383. * If appropriate, check DAC only. If not appropriate, or
  384. * short-cut DAC fails, then call permission() to do more
  385. * complete permission check.
  386. */
  387. static int exec_permission_lite(struct inode *inode)
  388. {
  389. umode_t mode = inode->i_mode;
  390. if (inode->i_op && inode->i_op->permission)
  391. return -EAGAIN;
  392. if (current->fsuid == inode->i_uid)
  393. mode >>= 6;
  394. else if (in_group_p(inode->i_gid))
  395. mode >>= 3;
  396. if (mode & MAY_EXEC)
  397. goto ok;
  398. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  399. goto ok;
  400. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  401. goto ok;
  402. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  403. goto ok;
  404. return -EACCES;
  405. ok:
  406. return security_inode_permission(inode, MAY_EXEC);
  407. }
  408. /*
  409. * This is called when everything else fails, and we actually have
  410. * to go to the low-level filesystem to find out what we should do..
  411. *
  412. * We get the directory semaphore, and after getting that we also
  413. * make sure that nobody added the entry to the dcache in the meantime..
  414. * SMP-safe
  415. */
  416. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  417. {
  418. struct dentry * result;
  419. struct inode *dir = parent->d_inode;
  420. mutex_lock(&dir->i_mutex);
  421. /*
  422. * First re-do the cached lookup just in case it was created
  423. * while we waited for the directory semaphore..
  424. *
  425. * FIXME! This could use version numbering or similar to
  426. * avoid unnecessary cache lookups.
  427. *
  428. * The "dcache_lock" is purely to protect the RCU list walker
  429. * from concurrent renames at this point (we mustn't get false
  430. * negatives from the RCU list walk here, unlike the optimistic
  431. * fast walk).
  432. *
  433. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  434. */
  435. result = d_lookup(parent, name);
  436. if (!result) {
  437. struct dentry *dentry;
  438. /* Don't create child dentry for a dead directory. */
  439. result = ERR_PTR(-ENOENT);
  440. if (IS_DEADDIR(dir))
  441. goto out_unlock;
  442. dentry = d_alloc(parent, name);
  443. result = ERR_PTR(-ENOMEM);
  444. if (dentry) {
  445. result = dir->i_op->lookup(dir, dentry, nd);
  446. if (result)
  447. dput(dentry);
  448. else
  449. result = dentry;
  450. }
  451. out_unlock:
  452. mutex_unlock(&dir->i_mutex);
  453. return result;
  454. }
  455. /*
  456. * Uhhuh! Nasty case: the cache was re-populated while
  457. * we waited on the semaphore. Need to revalidate.
  458. */
  459. mutex_unlock(&dir->i_mutex);
  460. if (result->d_op && result->d_op->d_revalidate) {
  461. result = do_revalidate(result, nd);
  462. if (!result)
  463. result = ERR_PTR(-ENOENT);
  464. }
  465. return result;
  466. }
  467. /* SMP-safe */
  468. static __always_inline void
  469. walk_init_root(const char *name, struct nameidata *nd)
  470. {
  471. struct fs_struct *fs = current->fs;
  472. read_lock(&fs->lock);
  473. nd->path = fs->root;
  474. path_get(&fs->root);
  475. read_unlock(&fs->lock);
  476. }
  477. /*
  478. * Wrapper to retry pathname resolution whenever the underlying
  479. * file system returns an ESTALE.
  480. *
  481. * Retry the whole path once, forcing real lookup requests
  482. * instead of relying on the dcache.
  483. */
  484. static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
  485. {
  486. struct path save = nd->path;
  487. int result;
  488. /* make sure the stuff we saved doesn't go away */
  489. path_get(&save);
  490. result = __link_path_walk(name, nd);
  491. if (result == -ESTALE) {
  492. /* nd->path had been dropped */
  493. nd->path = save;
  494. path_get(&nd->path);
  495. nd->flags |= LOOKUP_REVAL;
  496. result = __link_path_walk(name, nd);
  497. }
  498. path_put(&save);
  499. return result;
  500. }
  501. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  502. {
  503. int res = 0;
  504. char *name;
  505. if (IS_ERR(link))
  506. goto fail;
  507. if (*link == '/') {
  508. path_put(&nd->path);
  509. walk_init_root(link, nd);
  510. }
  511. res = link_path_walk(link, nd);
  512. if (nd->depth || res || nd->last_type!=LAST_NORM)
  513. return res;
  514. /*
  515. * If it is an iterative symlinks resolution in open_namei() we
  516. * have to copy the last component. And all that crap because of
  517. * bloody create() on broken symlinks. Furrfu...
  518. */
  519. name = __getname();
  520. if (unlikely(!name)) {
  521. path_put(&nd->path);
  522. return -ENOMEM;
  523. }
  524. strcpy(name, nd->last.name);
  525. nd->last.name = name;
  526. return 0;
  527. fail:
  528. path_put(&nd->path);
  529. return PTR_ERR(link);
  530. }
  531. static void path_put_conditional(struct path *path, struct nameidata *nd)
  532. {
  533. dput(path->dentry);
  534. if (path->mnt != nd->path.mnt)
  535. mntput(path->mnt);
  536. }
  537. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  538. {
  539. dput(nd->path.dentry);
  540. if (nd->path.mnt != path->mnt)
  541. mntput(nd->path.mnt);
  542. nd->path.mnt = path->mnt;
  543. nd->path.dentry = path->dentry;
  544. }
  545. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  546. {
  547. int error;
  548. void *cookie;
  549. struct dentry *dentry = path->dentry;
  550. touch_atime(path->mnt, dentry);
  551. nd_set_link(nd, NULL);
  552. if (path->mnt != nd->path.mnt) {
  553. path_to_nameidata(path, nd);
  554. dget(dentry);
  555. }
  556. mntget(path->mnt);
  557. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  558. error = PTR_ERR(cookie);
  559. if (!IS_ERR(cookie)) {
  560. char *s = nd_get_link(nd);
  561. error = 0;
  562. if (s)
  563. error = __vfs_follow_link(nd, s);
  564. if (dentry->d_inode->i_op->put_link)
  565. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  566. }
  567. path_put(path);
  568. return error;
  569. }
  570. /*
  571. * This limits recursive symlink follows to 8, while
  572. * limiting consecutive symlinks to 40.
  573. *
  574. * Without that kind of total limit, nasty chains of consecutive
  575. * symlinks can cause almost arbitrarily long lookups.
  576. */
  577. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  578. {
  579. int err = -ELOOP;
  580. if (current->link_count >= MAX_NESTED_LINKS)
  581. goto loop;
  582. if (current->total_link_count >= 40)
  583. goto loop;
  584. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  585. cond_resched();
  586. err = security_inode_follow_link(path->dentry, nd);
  587. if (err)
  588. goto loop;
  589. current->link_count++;
  590. current->total_link_count++;
  591. nd->depth++;
  592. err = __do_follow_link(path, nd);
  593. current->link_count--;
  594. nd->depth--;
  595. return err;
  596. loop:
  597. path_put_conditional(path, nd);
  598. path_put(&nd->path);
  599. return err;
  600. }
  601. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  602. {
  603. struct vfsmount *parent;
  604. struct dentry *mountpoint;
  605. spin_lock(&vfsmount_lock);
  606. parent=(*mnt)->mnt_parent;
  607. if (parent == *mnt) {
  608. spin_unlock(&vfsmount_lock);
  609. return 0;
  610. }
  611. mntget(parent);
  612. mountpoint=dget((*mnt)->mnt_mountpoint);
  613. spin_unlock(&vfsmount_lock);
  614. dput(*dentry);
  615. *dentry = mountpoint;
  616. mntput(*mnt);
  617. *mnt = parent;
  618. return 1;
  619. }
  620. /* no need for dcache_lock, as serialization is taken care in
  621. * namespace.c
  622. */
  623. static int __follow_mount(struct path *path)
  624. {
  625. int res = 0;
  626. while (d_mountpoint(path->dentry)) {
  627. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  628. if (!mounted)
  629. break;
  630. dput(path->dentry);
  631. if (res)
  632. mntput(path->mnt);
  633. path->mnt = mounted;
  634. path->dentry = dget(mounted->mnt_root);
  635. res = 1;
  636. }
  637. return res;
  638. }
  639. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  640. {
  641. while (d_mountpoint(*dentry)) {
  642. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  643. if (!mounted)
  644. break;
  645. dput(*dentry);
  646. mntput(*mnt);
  647. *mnt = mounted;
  648. *dentry = dget(mounted->mnt_root);
  649. }
  650. }
  651. /* no need for dcache_lock, as serialization is taken care in
  652. * namespace.c
  653. */
  654. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  655. {
  656. struct vfsmount *mounted;
  657. mounted = lookup_mnt(*mnt, *dentry);
  658. if (mounted) {
  659. dput(*dentry);
  660. mntput(*mnt);
  661. *mnt = mounted;
  662. *dentry = dget(mounted->mnt_root);
  663. return 1;
  664. }
  665. return 0;
  666. }
  667. static __always_inline void follow_dotdot(struct nameidata *nd)
  668. {
  669. struct fs_struct *fs = current->fs;
  670. while(1) {
  671. struct vfsmount *parent;
  672. struct dentry *old = nd->path.dentry;
  673. read_lock(&fs->lock);
  674. if (nd->path.dentry == fs->root.dentry &&
  675. nd->path.mnt == fs->root.mnt) {
  676. read_unlock(&fs->lock);
  677. break;
  678. }
  679. read_unlock(&fs->lock);
  680. spin_lock(&dcache_lock);
  681. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  682. nd->path.dentry = dget(nd->path.dentry->d_parent);
  683. spin_unlock(&dcache_lock);
  684. dput(old);
  685. break;
  686. }
  687. spin_unlock(&dcache_lock);
  688. spin_lock(&vfsmount_lock);
  689. parent = nd->path.mnt->mnt_parent;
  690. if (parent == nd->path.mnt) {
  691. spin_unlock(&vfsmount_lock);
  692. break;
  693. }
  694. mntget(parent);
  695. nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
  696. spin_unlock(&vfsmount_lock);
  697. dput(old);
  698. mntput(nd->path.mnt);
  699. nd->path.mnt = parent;
  700. }
  701. follow_mount(&nd->path.mnt, &nd->path.dentry);
  702. }
  703. /*
  704. * It's more convoluted than I'd like it to be, but... it's still fairly
  705. * small and for now I'd prefer to have fast path as straight as possible.
  706. * It _is_ time-critical.
  707. */
  708. static int do_lookup(struct nameidata *nd, struct qstr *name,
  709. struct path *path)
  710. {
  711. struct vfsmount *mnt = nd->path.mnt;
  712. struct dentry *dentry = __d_lookup(nd->path.dentry, name);
  713. if (!dentry)
  714. goto need_lookup;
  715. if (dentry->d_op && dentry->d_op->d_revalidate)
  716. goto need_revalidate;
  717. done:
  718. path->mnt = mnt;
  719. path->dentry = dentry;
  720. __follow_mount(path);
  721. return 0;
  722. need_lookup:
  723. dentry = real_lookup(nd->path.dentry, name, nd);
  724. if (IS_ERR(dentry))
  725. goto fail;
  726. goto done;
  727. need_revalidate:
  728. dentry = do_revalidate(dentry, nd);
  729. if (!dentry)
  730. goto need_lookup;
  731. if (IS_ERR(dentry))
  732. goto fail;
  733. goto done;
  734. fail:
  735. return PTR_ERR(dentry);
  736. }
  737. /*
  738. * Name resolution.
  739. * This is the basic name resolution function, turning a pathname into
  740. * the final dentry. We expect 'base' to be positive and a directory.
  741. *
  742. * Returns 0 and nd will have valid dentry and mnt on success.
  743. * Returns error and drops reference to input namei data on failure.
  744. */
  745. static int __link_path_walk(const char *name, struct nameidata *nd)
  746. {
  747. struct path next;
  748. struct inode *inode;
  749. int err;
  750. unsigned int lookup_flags = nd->flags;
  751. while (*name=='/')
  752. name++;
  753. if (!*name)
  754. goto return_reval;
  755. inode = nd->path.dentry->d_inode;
  756. if (nd->depth)
  757. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  758. /* At this point we know we have a real path component. */
  759. for(;;) {
  760. unsigned long hash;
  761. struct qstr this;
  762. unsigned int c;
  763. nd->flags |= LOOKUP_CONTINUE;
  764. err = exec_permission_lite(inode);
  765. if (err == -EAGAIN)
  766. err = vfs_permission(nd, MAY_EXEC);
  767. if (err)
  768. break;
  769. this.name = name;
  770. c = *(const unsigned char *)name;
  771. hash = init_name_hash();
  772. do {
  773. name++;
  774. hash = partial_name_hash(c, hash);
  775. c = *(const unsigned char *)name;
  776. } while (c && (c != '/'));
  777. this.len = name - (const char *) this.name;
  778. this.hash = end_name_hash(hash);
  779. /* remove trailing slashes? */
  780. if (!c)
  781. goto last_component;
  782. while (*++name == '/');
  783. if (!*name)
  784. goto last_with_slashes;
  785. /*
  786. * "." and ".." are special - ".." especially so because it has
  787. * to be able to know about the current root directory and
  788. * parent relationships.
  789. */
  790. if (this.name[0] == '.') switch (this.len) {
  791. default:
  792. break;
  793. case 2:
  794. if (this.name[1] != '.')
  795. break;
  796. follow_dotdot(nd);
  797. inode = nd->path.dentry->d_inode;
  798. /* fallthrough */
  799. case 1:
  800. continue;
  801. }
  802. /*
  803. * See if the low-level filesystem might want
  804. * to use its own hash..
  805. */
  806. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  807. err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
  808. &this);
  809. if (err < 0)
  810. break;
  811. }
  812. /* This does the actual lookups.. */
  813. err = do_lookup(nd, &this, &next);
  814. if (err)
  815. break;
  816. err = -ENOENT;
  817. inode = next.dentry->d_inode;
  818. if (!inode)
  819. goto out_dput;
  820. err = -ENOTDIR;
  821. if (!inode->i_op)
  822. goto out_dput;
  823. if (inode->i_op->follow_link) {
  824. err = do_follow_link(&next, nd);
  825. if (err)
  826. goto return_err;
  827. err = -ENOENT;
  828. inode = nd->path.dentry->d_inode;
  829. if (!inode)
  830. break;
  831. err = -ENOTDIR;
  832. if (!inode->i_op)
  833. break;
  834. } else
  835. path_to_nameidata(&next, nd);
  836. err = -ENOTDIR;
  837. if (!inode->i_op->lookup)
  838. break;
  839. continue;
  840. /* here ends the main loop */
  841. last_with_slashes:
  842. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  843. last_component:
  844. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  845. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  846. if (lookup_flags & LOOKUP_PARENT)
  847. goto lookup_parent;
  848. if (this.name[0] == '.') switch (this.len) {
  849. default:
  850. break;
  851. case 2:
  852. if (this.name[1] != '.')
  853. break;
  854. follow_dotdot(nd);
  855. inode = nd->path.dentry->d_inode;
  856. /* fallthrough */
  857. case 1:
  858. goto return_reval;
  859. }
  860. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  861. err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
  862. &this);
  863. if (err < 0)
  864. break;
  865. }
  866. err = do_lookup(nd, &this, &next);
  867. if (err)
  868. break;
  869. inode = next.dentry->d_inode;
  870. if ((lookup_flags & LOOKUP_FOLLOW)
  871. && inode && inode->i_op && inode->i_op->follow_link) {
  872. err = do_follow_link(&next, nd);
  873. if (err)
  874. goto return_err;
  875. inode = nd->path.dentry->d_inode;
  876. } else
  877. path_to_nameidata(&next, nd);
  878. err = -ENOENT;
  879. if (!inode)
  880. break;
  881. if (lookup_flags & LOOKUP_DIRECTORY) {
  882. err = -ENOTDIR;
  883. if (!inode->i_op || !inode->i_op->lookup)
  884. break;
  885. }
  886. goto return_base;
  887. lookup_parent:
  888. nd->last = this;
  889. nd->last_type = LAST_NORM;
  890. if (this.name[0] != '.')
  891. goto return_base;
  892. if (this.len == 1)
  893. nd->last_type = LAST_DOT;
  894. else if (this.len == 2 && this.name[1] == '.')
  895. nd->last_type = LAST_DOTDOT;
  896. else
  897. goto return_base;
  898. return_reval:
  899. /*
  900. * We bypassed the ordinary revalidation routines.
  901. * We may need to check the cached dentry for staleness.
  902. */
  903. if (nd->path.dentry && nd->path.dentry->d_sb &&
  904. (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  905. err = -ESTALE;
  906. /* Note: we do not d_invalidate() */
  907. if (!nd->path.dentry->d_op->d_revalidate(
  908. nd->path.dentry, nd))
  909. break;
  910. }
  911. return_base:
  912. return 0;
  913. out_dput:
  914. path_put_conditional(&next, nd);
  915. break;
  916. }
  917. path_put(&nd->path);
  918. return_err:
  919. return err;
  920. }
  921. static int path_walk(const char *name, struct nameidata *nd)
  922. {
  923. current->total_link_count = 0;
  924. return link_path_walk(name, nd);
  925. }
  926. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  927. static int do_path_lookup(int dfd, const char *name,
  928. unsigned int flags, struct nameidata *nd)
  929. {
  930. int retval = 0;
  931. int fput_needed;
  932. struct file *file;
  933. struct fs_struct *fs = current->fs;
  934. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  935. nd->flags = flags;
  936. nd->depth = 0;
  937. if (*name=='/') {
  938. read_lock(&fs->lock);
  939. nd->path = fs->root;
  940. path_get(&fs->root);
  941. read_unlock(&fs->lock);
  942. } else if (dfd == AT_FDCWD) {
  943. read_lock(&fs->lock);
  944. nd->path = fs->pwd;
  945. path_get(&fs->pwd);
  946. read_unlock(&fs->lock);
  947. } else {
  948. struct dentry *dentry;
  949. file = fget_light(dfd, &fput_needed);
  950. retval = -EBADF;
  951. if (!file)
  952. goto out_fail;
  953. dentry = file->f_path.dentry;
  954. retval = -ENOTDIR;
  955. if (!S_ISDIR(dentry->d_inode->i_mode))
  956. goto fput_fail;
  957. retval = file_permission(file, MAY_EXEC);
  958. if (retval)
  959. goto fput_fail;
  960. nd->path = file->f_path;
  961. path_get(&file->f_path);
  962. fput_light(file, fput_needed);
  963. }
  964. retval = path_walk(name, nd);
  965. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  966. nd->path.dentry->d_inode))
  967. audit_inode(name, nd->path.dentry);
  968. out_fail:
  969. return retval;
  970. fput_fail:
  971. fput_light(file, fput_needed);
  972. goto out_fail;
  973. }
  974. int path_lookup(const char *name, unsigned int flags,
  975. struct nameidata *nd)
  976. {
  977. return do_path_lookup(AT_FDCWD, name, flags, nd);
  978. }
  979. int kern_path(const char *name, unsigned int flags, struct path *path)
  980. {
  981. struct nameidata nd;
  982. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  983. if (!res)
  984. *path = nd.path;
  985. return res;
  986. }
  987. /**
  988. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  989. * @dentry: pointer to dentry of the base directory
  990. * @mnt: pointer to vfs mount of the base directory
  991. * @name: pointer to file name
  992. * @flags: lookup flags
  993. * @nd: pointer to nameidata
  994. */
  995. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  996. const char *name, unsigned int flags,
  997. struct nameidata *nd)
  998. {
  999. int retval;
  1000. /* same as do_path_lookup */
  1001. nd->last_type = LAST_ROOT;
  1002. nd->flags = flags;
  1003. nd->depth = 0;
  1004. nd->path.dentry = dentry;
  1005. nd->path.mnt = mnt;
  1006. path_get(&nd->path);
  1007. retval = path_walk(name, nd);
  1008. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  1009. nd->path.dentry->d_inode))
  1010. audit_inode(name, nd->path.dentry);
  1011. return retval;
  1012. }
  1013. /**
  1014. * path_lookup_open - lookup a file path with open intent
  1015. * @dfd: the directory to use as base, or AT_FDCWD
  1016. * @name: pointer to file name
  1017. * @lookup_flags: lookup intent flags
  1018. * @nd: pointer to nameidata
  1019. * @open_flags: open intent flags
  1020. */
  1021. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1022. struct nameidata *nd, int open_flags)
  1023. {
  1024. struct file *filp = get_empty_filp();
  1025. int err;
  1026. if (filp == NULL)
  1027. return -ENFILE;
  1028. nd->intent.open.file = filp;
  1029. nd->intent.open.flags = open_flags;
  1030. nd->intent.open.create_mode = 0;
  1031. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1032. if (IS_ERR(nd->intent.open.file)) {
  1033. if (err == 0) {
  1034. err = PTR_ERR(nd->intent.open.file);
  1035. path_put(&nd->path);
  1036. }
  1037. } else if (err != 0)
  1038. release_open_intent(nd);
  1039. return err;
  1040. }
  1041. static struct dentry *__lookup_hash(struct qstr *name,
  1042. struct dentry *base, struct nameidata *nd)
  1043. {
  1044. struct dentry *dentry;
  1045. struct inode *inode;
  1046. int err;
  1047. inode = base->d_inode;
  1048. /*
  1049. * See if the low-level filesystem might want
  1050. * to use its own hash..
  1051. */
  1052. if (base->d_op && base->d_op->d_hash) {
  1053. err = base->d_op->d_hash(base, name);
  1054. dentry = ERR_PTR(err);
  1055. if (err < 0)
  1056. goto out;
  1057. }
  1058. dentry = cached_lookup(base, name, nd);
  1059. if (!dentry) {
  1060. struct dentry *new;
  1061. /* Don't create child dentry for a dead directory. */
  1062. dentry = ERR_PTR(-ENOENT);
  1063. if (IS_DEADDIR(inode))
  1064. goto out;
  1065. new = d_alloc(base, name);
  1066. dentry = ERR_PTR(-ENOMEM);
  1067. if (!new)
  1068. goto out;
  1069. dentry = inode->i_op->lookup(inode, new, nd);
  1070. if (!dentry)
  1071. dentry = new;
  1072. else
  1073. dput(new);
  1074. }
  1075. out:
  1076. return dentry;
  1077. }
  1078. /*
  1079. * Restricted form of lookup. Doesn't follow links, single-component only,
  1080. * needs parent already locked. Doesn't follow mounts.
  1081. * SMP-safe.
  1082. */
  1083. static struct dentry *lookup_hash(struct nameidata *nd)
  1084. {
  1085. int err;
  1086. err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
  1087. if (err)
  1088. return ERR_PTR(err);
  1089. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1090. }
  1091. static int __lookup_one_len(const char *name, struct qstr *this,
  1092. struct dentry *base, int len)
  1093. {
  1094. unsigned long hash;
  1095. unsigned int c;
  1096. this->name = name;
  1097. this->len = len;
  1098. if (!len)
  1099. return -EACCES;
  1100. hash = init_name_hash();
  1101. while (len--) {
  1102. c = *(const unsigned char *)name++;
  1103. if (c == '/' || c == '\0')
  1104. return -EACCES;
  1105. hash = partial_name_hash(c, hash);
  1106. }
  1107. this->hash = end_name_hash(hash);
  1108. return 0;
  1109. }
  1110. /**
  1111. * lookup_one_len - filesystem helper to lookup single pathname component
  1112. * @name: pathname component to lookup
  1113. * @base: base directory to lookup from
  1114. * @len: maximum length @len should be interpreted to
  1115. *
  1116. * Note that this routine is purely a helper for filesystem usage and should
  1117. * not be called by generic code. Also note that by using this function the
  1118. * nameidata argument is passed to the filesystem methods and a filesystem
  1119. * using this helper needs to be prepared for that.
  1120. */
  1121. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1122. {
  1123. int err;
  1124. struct qstr this;
  1125. err = __lookup_one_len(name, &this, base, len);
  1126. if (err)
  1127. return ERR_PTR(err);
  1128. err = inode_permission(base->d_inode, MAY_EXEC);
  1129. if (err)
  1130. return ERR_PTR(err);
  1131. return __lookup_hash(&this, base, NULL);
  1132. }
  1133. /**
  1134. * lookup_one_noperm - bad hack for sysfs
  1135. * @name: pathname component to lookup
  1136. * @base: base directory to lookup from
  1137. *
  1138. * This is a variant of lookup_one_len that doesn't perform any permission
  1139. * checks. It's a horrible hack to work around the braindead sysfs
  1140. * architecture and should not be used anywhere else.
  1141. *
  1142. * DON'T USE THIS FUNCTION EVER, thanks.
  1143. */
  1144. struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
  1145. {
  1146. int err;
  1147. struct qstr this;
  1148. err = __lookup_one_len(name, &this, base, strlen(name));
  1149. if (err)
  1150. return ERR_PTR(err);
  1151. return __lookup_hash(&this, base, NULL);
  1152. }
  1153. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1154. struct path *path)
  1155. {
  1156. struct nameidata nd;
  1157. char *tmp = getname(name);
  1158. int err = PTR_ERR(tmp);
  1159. if (!IS_ERR(tmp)) {
  1160. BUG_ON(flags & LOOKUP_PARENT);
  1161. err = do_path_lookup(dfd, tmp, flags, &nd);
  1162. putname(tmp);
  1163. if (!err)
  1164. *path = nd.path;
  1165. }
  1166. return err;
  1167. }
  1168. static int user_path_parent(int dfd, const char __user *path,
  1169. struct nameidata *nd, char **name)
  1170. {
  1171. char *s = getname(path);
  1172. int error;
  1173. if (IS_ERR(s))
  1174. return PTR_ERR(s);
  1175. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1176. if (error)
  1177. putname(s);
  1178. else
  1179. *name = s;
  1180. return error;
  1181. }
  1182. /*
  1183. * It's inline, so penalty for filesystems that don't use sticky bit is
  1184. * minimal.
  1185. */
  1186. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1187. {
  1188. if (!(dir->i_mode & S_ISVTX))
  1189. return 0;
  1190. if (inode->i_uid == current->fsuid)
  1191. return 0;
  1192. if (dir->i_uid == current->fsuid)
  1193. return 0;
  1194. return !capable(CAP_FOWNER);
  1195. }
  1196. /*
  1197. * Check whether we can remove a link victim from directory dir, check
  1198. * whether the type of victim is right.
  1199. * 1. We can't do it if dir is read-only (done in permission())
  1200. * 2. We should have write and exec permissions on dir
  1201. * 3. We can't remove anything from append-only dir
  1202. * 4. We can't do anything with immutable dir (done in permission())
  1203. * 5. If the sticky bit on dir is set we should either
  1204. * a. be owner of dir, or
  1205. * b. be owner of victim, or
  1206. * c. have CAP_FOWNER capability
  1207. * 6. If the victim is append-only or immutable we can't do antyhing with
  1208. * links pointing to it.
  1209. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1210. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1211. * 9. We can't remove a root or mountpoint.
  1212. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1213. * nfs_async_unlink().
  1214. */
  1215. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1216. {
  1217. int error;
  1218. if (!victim->d_inode)
  1219. return -ENOENT;
  1220. BUG_ON(victim->d_parent->d_inode != dir);
  1221. audit_inode_child(victim->d_name.name, victim, dir);
  1222. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1223. if (error)
  1224. return error;
  1225. if (IS_APPEND(dir))
  1226. return -EPERM;
  1227. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1228. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1229. return -EPERM;
  1230. if (isdir) {
  1231. if (!S_ISDIR(victim->d_inode->i_mode))
  1232. return -ENOTDIR;
  1233. if (IS_ROOT(victim))
  1234. return -EBUSY;
  1235. } else if (S_ISDIR(victim->d_inode->i_mode))
  1236. return -EISDIR;
  1237. if (IS_DEADDIR(dir))
  1238. return -ENOENT;
  1239. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1240. return -EBUSY;
  1241. return 0;
  1242. }
  1243. /* Check whether we can create an object with dentry child in directory
  1244. * dir.
  1245. * 1. We can't do it if child already exists (open has special treatment for
  1246. * this case, but since we are inlined it's OK)
  1247. * 2. We can't do it if dir is read-only (done in permission())
  1248. * 3. We should have write and exec permissions on dir
  1249. * 4. We can't do it if dir is immutable (done in permission())
  1250. */
  1251. static inline int may_create(struct inode *dir, struct dentry *child)
  1252. {
  1253. if (child->d_inode)
  1254. return -EEXIST;
  1255. if (IS_DEADDIR(dir))
  1256. return -ENOENT;
  1257. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1258. }
  1259. /*
  1260. * O_DIRECTORY translates into forcing a directory lookup.
  1261. */
  1262. static inline int lookup_flags(unsigned int f)
  1263. {
  1264. unsigned long retval = LOOKUP_FOLLOW;
  1265. if (f & O_NOFOLLOW)
  1266. retval &= ~LOOKUP_FOLLOW;
  1267. if (f & O_DIRECTORY)
  1268. retval |= LOOKUP_DIRECTORY;
  1269. return retval;
  1270. }
  1271. /*
  1272. * p1 and p2 should be directories on the same fs.
  1273. */
  1274. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1275. {
  1276. struct dentry *p;
  1277. if (p1 == p2) {
  1278. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1279. return NULL;
  1280. }
  1281. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1282. p = d_ancestor(p2, p1);
  1283. if (p) {
  1284. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1285. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1286. return p;
  1287. }
  1288. p = d_ancestor(p1, p2);
  1289. if (p) {
  1290. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1291. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1292. return p;
  1293. }
  1294. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1295. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1296. return NULL;
  1297. }
  1298. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1299. {
  1300. mutex_unlock(&p1->d_inode->i_mutex);
  1301. if (p1 != p2) {
  1302. mutex_unlock(&p2->d_inode->i_mutex);
  1303. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1304. }
  1305. }
  1306. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1307. struct nameidata *nd)
  1308. {
  1309. int error = may_create(dir, dentry);
  1310. if (error)
  1311. return error;
  1312. if (!dir->i_op || !dir->i_op->create)
  1313. return -EACCES; /* shouldn't it be ENOSYS? */
  1314. mode &= S_IALLUGO;
  1315. mode |= S_IFREG;
  1316. error = security_inode_create(dir, dentry, mode);
  1317. if (error)
  1318. return error;
  1319. DQUOT_INIT(dir);
  1320. error = dir->i_op->create(dir, dentry, mode, nd);
  1321. if (!error)
  1322. fsnotify_create(dir, dentry);
  1323. return error;
  1324. }
  1325. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1326. {
  1327. struct dentry *dentry = nd->path.dentry;
  1328. struct inode *inode = dentry->d_inode;
  1329. int error;
  1330. if (!inode)
  1331. return -ENOENT;
  1332. if (S_ISLNK(inode->i_mode))
  1333. return -ELOOP;
  1334. if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
  1335. return -EISDIR;
  1336. /*
  1337. * FIFO's, sockets and device files are special: they don't
  1338. * actually live on the filesystem itself, and as such you
  1339. * can write to them even if the filesystem is read-only.
  1340. */
  1341. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1342. flag &= ~O_TRUNC;
  1343. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1344. if (nd->path.mnt->mnt_flags & MNT_NODEV)
  1345. return -EACCES;
  1346. flag &= ~O_TRUNC;
  1347. }
  1348. error = vfs_permission(nd, acc_mode);
  1349. if (error)
  1350. return error;
  1351. /*
  1352. * An append-only file must be opened in append mode for writing.
  1353. */
  1354. if (IS_APPEND(inode)) {
  1355. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1356. return -EPERM;
  1357. if (flag & O_TRUNC)
  1358. return -EPERM;
  1359. }
  1360. /* O_NOATIME can only be set by the owner or superuser */
  1361. if (flag & O_NOATIME)
  1362. if (!is_owner_or_cap(inode))
  1363. return -EPERM;
  1364. /*
  1365. * Ensure there are no outstanding leases on the file.
  1366. */
  1367. error = break_lease(inode, flag);
  1368. if (error)
  1369. return error;
  1370. if (flag & O_TRUNC) {
  1371. error = get_write_access(inode);
  1372. if (error)
  1373. return error;
  1374. /*
  1375. * Refuse to truncate files with mandatory locks held on them.
  1376. */
  1377. error = locks_verify_locked(inode);
  1378. if (!error) {
  1379. DQUOT_INIT(inode);
  1380. error = do_truncate(dentry, 0,
  1381. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1382. NULL);
  1383. }
  1384. put_write_access(inode);
  1385. if (error)
  1386. return error;
  1387. } else
  1388. if (flag & FMODE_WRITE)
  1389. DQUOT_INIT(inode);
  1390. return 0;
  1391. }
  1392. /*
  1393. * Be careful about ever adding any more callers of this
  1394. * function. Its flags must be in the namei format, not
  1395. * what get passed to sys_open().
  1396. */
  1397. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1398. int flag, int mode)
  1399. {
  1400. int error;
  1401. struct dentry *dir = nd->path.dentry;
  1402. if (!IS_POSIXACL(dir->d_inode))
  1403. mode &= ~current->fs->umask;
  1404. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1405. mutex_unlock(&dir->d_inode->i_mutex);
  1406. dput(nd->path.dentry);
  1407. nd->path.dentry = path->dentry;
  1408. if (error)
  1409. return error;
  1410. /* Don't check for write permission, don't truncate */
  1411. return may_open(nd, 0, flag & ~O_TRUNC);
  1412. }
  1413. /*
  1414. * Note that while the flag value (low two bits) for sys_open means:
  1415. * 00 - read-only
  1416. * 01 - write-only
  1417. * 10 - read-write
  1418. * 11 - special
  1419. * it is changed into
  1420. * 00 - no permissions needed
  1421. * 01 - read-permission
  1422. * 10 - write-permission
  1423. * 11 - read-write
  1424. * for the internal routines (ie open_namei()/follow_link() etc)
  1425. * This is more logical, and also allows the 00 "no perm needed"
  1426. * to be used for symlinks (where the permissions are checked
  1427. * later).
  1428. *
  1429. */
  1430. static inline int open_to_namei_flags(int flag)
  1431. {
  1432. if ((flag+1) & O_ACCMODE)
  1433. flag++;
  1434. return flag;
  1435. }
  1436. static int open_will_write_to_fs(int flag, struct inode *inode)
  1437. {
  1438. /*
  1439. * We'll never write to the fs underlying
  1440. * a device file.
  1441. */
  1442. if (special_file(inode->i_mode))
  1443. return 0;
  1444. return (flag & O_TRUNC);
  1445. }
  1446. /*
  1447. * Note that the low bits of the passed in "open_flag"
  1448. * are not the same as in the local variable "flag". See
  1449. * open_to_namei_flags() for more details.
  1450. */
  1451. struct file *do_filp_open(int dfd, const char *pathname,
  1452. int open_flag, int mode)
  1453. {
  1454. struct file *filp;
  1455. struct nameidata nd;
  1456. int acc_mode, error;
  1457. struct path path;
  1458. struct dentry *dir;
  1459. int count = 0;
  1460. int will_write;
  1461. int flag = open_to_namei_flags(open_flag);
  1462. acc_mode = MAY_OPEN | ACC_MODE(flag);
  1463. /* O_TRUNC implies we need access checks for write permissions */
  1464. if (flag & O_TRUNC)
  1465. acc_mode |= MAY_WRITE;
  1466. /* Allow the LSM permission hook to distinguish append
  1467. access from general write access. */
  1468. if (flag & O_APPEND)
  1469. acc_mode |= MAY_APPEND;
  1470. /*
  1471. * The simplest case - just a plain lookup.
  1472. */
  1473. if (!(flag & O_CREAT)) {
  1474. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1475. &nd, flag);
  1476. if (error)
  1477. return ERR_PTR(error);
  1478. goto ok;
  1479. }
  1480. /*
  1481. * Create - we need to know the parent.
  1482. */
  1483. error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  1484. if (error)
  1485. return ERR_PTR(error);
  1486. /*
  1487. * We have the parent and last component. First of all, check
  1488. * that we are not asked to creat(2) an obvious directory - that
  1489. * will not do.
  1490. */
  1491. error = -EISDIR;
  1492. if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
  1493. goto exit_parent;
  1494. error = -ENFILE;
  1495. filp = get_empty_filp();
  1496. if (filp == NULL)
  1497. goto exit_parent;
  1498. nd.intent.open.file = filp;
  1499. nd.intent.open.flags = flag;
  1500. nd.intent.open.create_mode = mode;
  1501. dir = nd.path.dentry;
  1502. nd.flags &= ~LOOKUP_PARENT;
  1503. nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
  1504. if (flag & O_EXCL)
  1505. nd.flags |= LOOKUP_EXCL;
  1506. mutex_lock(&dir->d_inode->i_mutex);
  1507. path.dentry = lookup_hash(&nd);
  1508. path.mnt = nd.path.mnt;
  1509. do_last:
  1510. error = PTR_ERR(path.dentry);
  1511. if (IS_ERR(path.dentry)) {
  1512. mutex_unlock(&dir->d_inode->i_mutex);
  1513. goto exit;
  1514. }
  1515. if (IS_ERR(nd.intent.open.file)) {
  1516. error = PTR_ERR(nd.intent.open.file);
  1517. goto exit_mutex_unlock;
  1518. }
  1519. /* Negative dentry, just create the file */
  1520. if (!path.dentry->d_inode) {
  1521. /*
  1522. * This write is needed to ensure that a
  1523. * ro->rw transition does not occur between
  1524. * the time when the file is created and when
  1525. * a permanent write count is taken through
  1526. * the 'struct file' in nameidata_to_filp().
  1527. */
  1528. error = mnt_want_write(nd.path.mnt);
  1529. if (error)
  1530. goto exit_mutex_unlock;
  1531. error = __open_namei_create(&nd, &path, flag, mode);
  1532. if (error) {
  1533. mnt_drop_write(nd.path.mnt);
  1534. goto exit;
  1535. }
  1536. filp = nameidata_to_filp(&nd, open_flag);
  1537. mnt_drop_write(nd.path.mnt);
  1538. return filp;
  1539. }
  1540. /*
  1541. * It already exists.
  1542. */
  1543. mutex_unlock(&dir->d_inode->i_mutex);
  1544. audit_inode(pathname, path.dentry);
  1545. error = -EEXIST;
  1546. if (flag & O_EXCL)
  1547. goto exit_dput;
  1548. if (__follow_mount(&path)) {
  1549. error = -ELOOP;
  1550. if (flag & O_NOFOLLOW)
  1551. goto exit_dput;
  1552. }
  1553. error = -ENOENT;
  1554. if (!path.dentry->d_inode)
  1555. goto exit_dput;
  1556. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1557. goto do_link;
  1558. path_to_nameidata(&path, &nd);
  1559. error = -EISDIR;
  1560. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1561. goto exit;
  1562. ok:
  1563. /*
  1564. * Consider:
  1565. * 1. may_open() truncates a file
  1566. * 2. a rw->ro mount transition occurs
  1567. * 3. nameidata_to_filp() fails due to
  1568. * the ro mount.
  1569. * That would be inconsistent, and should
  1570. * be avoided. Taking this mnt write here
  1571. * ensures that (2) can not occur.
  1572. */
  1573. will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
  1574. if (will_write) {
  1575. error = mnt_want_write(nd.path.mnt);
  1576. if (error)
  1577. goto exit;
  1578. }
  1579. error = may_open(&nd, acc_mode, flag);
  1580. if (error) {
  1581. if (will_write)
  1582. mnt_drop_write(nd.path.mnt);
  1583. goto exit;
  1584. }
  1585. filp = nameidata_to_filp(&nd, open_flag);
  1586. /*
  1587. * It is now safe to drop the mnt write
  1588. * because the filp has had a write taken
  1589. * on its behalf.
  1590. */
  1591. if (will_write)
  1592. mnt_drop_write(nd.path.mnt);
  1593. return filp;
  1594. exit_mutex_unlock:
  1595. mutex_unlock(&dir->d_inode->i_mutex);
  1596. exit_dput:
  1597. path_put_conditional(&path, &nd);
  1598. exit:
  1599. if (!IS_ERR(nd.intent.open.file))
  1600. release_open_intent(&nd);
  1601. exit_parent:
  1602. path_put(&nd.path);
  1603. return ERR_PTR(error);
  1604. do_link:
  1605. error = -ELOOP;
  1606. if (flag & O_NOFOLLOW)
  1607. goto exit_dput;
  1608. /*
  1609. * This is subtle. Instead of calling do_follow_link() we do the
  1610. * thing by hands. The reason is that this way we have zero link_count
  1611. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1612. * After that we have the parent and last component, i.e.
  1613. * we are in the same situation as after the first path_walk().
  1614. * Well, almost - if the last component is normal we get its copy
  1615. * stored in nd->last.name and we will have to putname() it when we
  1616. * are done. Procfs-like symlinks just set LAST_BIND.
  1617. */
  1618. nd.flags |= LOOKUP_PARENT;
  1619. error = security_inode_follow_link(path.dentry, &nd);
  1620. if (error)
  1621. goto exit_dput;
  1622. error = __do_follow_link(&path, &nd);
  1623. if (error) {
  1624. /* Does someone understand code flow here? Or it is only
  1625. * me so stupid? Anathema to whoever designed this non-sense
  1626. * with "intent.open".
  1627. */
  1628. release_open_intent(&nd);
  1629. return ERR_PTR(error);
  1630. }
  1631. nd.flags &= ~LOOKUP_PARENT;
  1632. if (nd.last_type == LAST_BIND)
  1633. goto ok;
  1634. error = -EISDIR;
  1635. if (nd.last_type != LAST_NORM)
  1636. goto exit;
  1637. if (nd.last.name[nd.last.len]) {
  1638. __putname(nd.last.name);
  1639. goto exit;
  1640. }
  1641. error = -ELOOP;
  1642. if (count++==32) {
  1643. __putname(nd.last.name);
  1644. goto exit;
  1645. }
  1646. dir = nd.path.dentry;
  1647. mutex_lock(&dir->d_inode->i_mutex);
  1648. path.dentry = lookup_hash(&nd);
  1649. path.mnt = nd.path.mnt;
  1650. __putname(nd.last.name);
  1651. goto do_last;
  1652. }
  1653. /**
  1654. * filp_open - open file and return file pointer
  1655. *
  1656. * @filename: path to open
  1657. * @flags: open flags as per the open(2) second argument
  1658. * @mode: mode for the new file if O_CREAT is set, else ignored
  1659. *
  1660. * This is the helper to open a file from kernelspace if you really
  1661. * have to. But in generally you should not do this, so please move
  1662. * along, nothing to see here..
  1663. */
  1664. struct file *filp_open(const char *filename, int flags, int mode)
  1665. {
  1666. return do_filp_open(AT_FDCWD, filename, flags, mode);
  1667. }
  1668. EXPORT_SYMBOL(filp_open);
  1669. /**
  1670. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1671. * @nd: nameidata info
  1672. * @is_dir: directory flag
  1673. *
  1674. * Simple function to lookup and return a dentry and create it
  1675. * if it doesn't exist. Is SMP-safe.
  1676. *
  1677. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  1678. */
  1679. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1680. {
  1681. struct dentry *dentry = ERR_PTR(-EEXIST);
  1682. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1683. /*
  1684. * Yucky last component or no last component at all?
  1685. * (foo/., foo/.., /////)
  1686. */
  1687. if (nd->last_type != LAST_NORM)
  1688. goto fail;
  1689. nd->flags &= ~LOOKUP_PARENT;
  1690. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  1691. nd->intent.open.flags = O_EXCL;
  1692. /*
  1693. * Do the final lookup.
  1694. */
  1695. dentry = lookup_hash(nd);
  1696. if (IS_ERR(dentry))
  1697. goto fail;
  1698. if (dentry->d_inode)
  1699. goto eexist;
  1700. /*
  1701. * Special case - lookup gave negative, but... we had foo/bar/
  1702. * From the vfs_mknod() POV we just have a negative dentry -
  1703. * all is fine. Let's be bastards - you had / on the end, you've
  1704. * been asking for (non-existent) directory. -ENOENT for you.
  1705. */
  1706. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  1707. dput(dentry);
  1708. dentry = ERR_PTR(-ENOENT);
  1709. }
  1710. return dentry;
  1711. eexist:
  1712. dput(dentry);
  1713. dentry = ERR_PTR(-EEXIST);
  1714. fail:
  1715. return dentry;
  1716. }
  1717. EXPORT_SYMBOL_GPL(lookup_create);
  1718. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1719. {
  1720. int error = may_create(dir, dentry);
  1721. if (error)
  1722. return error;
  1723. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1724. return -EPERM;
  1725. if (!dir->i_op || !dir->i_op->mknod)
  1726. return -EPERM;
  1727. error = devcgroup_inode_mknod(mode, dev);
  1728. if (error)
  1729. return error;
  1730. error = security_inode_mknod(dir, dentry, mode, dev);
  1731. if (error)
  1732. return error;
  1733. DQUOT_INIT(dir);
  1734. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1735. if (!error)
  1736. fsnotify_create(dir, dentry);
  1737. return error;
  1738. }
  1739. static int may_mknod(mode_t mode)
  1740. {
  1741. switch (mode & S_IFMT) {
  1742. case S_IFREG:
  1743. case S_IFCHR:
  1744. case S_IFBLK:
  1745. case S_IFIFO:
  1746. case S_IFSOCK:
  1747. case 0: /* zero mode translates to S_IFREG */
  1748. return 0;
  1749. case S_IFDIR:
  1750. return -EPERM;
  1751. default:
  1752. return -EINVAL;
  1753. }
  1754. }
  1755. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1756. unsigned dev)
  1757. {
  1758. int error;
  1759. char *tmp;
  1760. struct dentry *dentry;
  1761. struct nameidata nd;
  1762. if (S_ISDIR(mode))
  1763. return -EPERM;
  1764. error = user_path_parent(dfd, filename, &nd, &tmp);
  1765. if (error)
  1766. return error;
  1767. dentry = lookup_create(&nd, 0);
  1768. if (IS_ERR(dentry)) {
  1769. error = PTR_ERR(dentry);
  1770. goto out_unlock;
  1771. }
  1772. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1773. mode &= ~current->fs->umask;
  1774. error = may_mknod(mode);
  1775. if (error)
  1776. goto out_dput;
  1777. error = mnt_want_write(nd.path.mnt);
  1778. if (error)
  1779. goto out_dput;
  1780. switch (mode & S_IFMT) {
  1781. case 0: case S_IFREG:
  1782. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  1783. break;
  1784. case S_IFCHR: case S_IFBLK:
  1785. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  1786. new_decode_dev(dev));
  1787. break;
  1788. case S_IFIFO: case S_IFSOCK:
  1789. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  1790. break;
  1791. }
  1792. mnt_drop_write(nd.path.mnt);
  1793. out_dput:
  1794. dput(dentry);
  1795. out_unlock:
  1796. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1797. path_put(&nd.path);
  1798. putname(tmp);
  1799. return error;
  1800. }
  1801. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1802. {
  1803. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1804. }
  1805. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1806. {
  1807. int error = may_create(dir, dentry);
  1808. if (error)
  1809. return error;
  1810. if (!dir->i_op || !dir->i_op->mkdir)
  1811. return -EPERM;
  1812. mode &= (S_IRWXUGO|S_ISVTX);
  1813. error = security_inode_mkdir(dir, dentry, mode);
  1814. if (error)
  1815. return error;
  1816. DQUOT_INIT(dir);
  1817. error = dir->i_op->mkdir(dir, dentry, mode);
  1818. if (!error)
  1819. fsnotify_mkdir(dir, dentry);
  1820. return error;
  1821. }
  1822. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1823. {
  1824. int error = 0;
  1825. char * tmp;
  1826. struct dentry *dentry;
  1827. struct nameidata nd;
  1828. error = user_path_parent(dfd, pathname, &nd, &tmp);
  1829. if (error)
  1830. goto out_err;
  1831. dentry = lookup_create(&nd, 1);
  1832. error = PTR_ERR(dentry);
  1833. if (IS_ERR(dentry))
  1834. goto out_unlock;
  1835. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1836. mode &= ~current->fs->umask;
  1837. error = mnt_want_write(nd.path.mnt);
  1838. if (error)
  1839. goto out_dput;
  1840. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  1841. mnt_drop_write(nd.path.mnt);
  1842. out_dput:
  1843. dput(dentry);
  1844. out_unlock:
  1845. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1846. path_put(&nd.path);
  1847. putname(tmp);
  1848. out_err:
  1849. return error;
  1850. }
  1851. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1852. {
  1853. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1854. }
  1855. /*
  1856. * We try to drop the dentry early: we should have
  1857. * a usage count of 2 if we're the only user of this
  1858. * dentry, and if that is true (possibly after pruning
  1859. * the dcache), then we drop the dentry now.
  1860. *
  1861. * A low-level filesystem can, if it choses, legally
  1862. * do a
  1863. *
  1864. * if (!d_unhashed(dentry))
  1865. * return -EBUSY;
  1866. *
  1867. * if it cannot handle the case of removing a directory
  1868. * that is still in use by something else..
  1869. */
  1870. void dentry_unhash(struct dentry *dentry)
  1871. {
  1872. dget(dentry);
  1873. shrink_dcache_parent(dentry);
  1874. spin_lock(&dcache_lock);
  1875. spin_lock(&dentry->d_lock);
  1876. if (atomic_read(&dentry->d_count) == 2)
  1877. __d_drop(dentry);
  1878. spin_unlock(&dentry->d_lock);
  1879. spin_unlock(&dcache_lock);
  1880. }
  1881. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1882. {
  1883. int error = may_delete(dir, dentry, 1);
  1884. if (error)
  1885. return error;
  1886. if (!dir->i_op || !dir->i_op->rmdir)
  1887. return -EPERM;
  1888. DQUOT_INIT(dir);
  1889. mutex_lock(&dentry->d_inode->i_mutex);
  1890. dentry_unhash(dentry);
  1891. if (d_mountpoint(dentry))
  1892. error = -EBUSY;
  1893. else {
  1894. error = security_inode_rmdir(dir, dentry);
  1895. if (!error) {
  1896. error = dir->i_op->rmdir(dir, dentry);
  1897. if (!error)
  1898. dentry->d_inode->i_flags |= S_DEAD;
  1899. }
  1900. }
  1901. mutex_unlock(&dentry->d_inode->i_mutex);
  1902. if (!error) {
  1903. d_delete(dentry);
  1904. }
  1905. dput(dentry);
  1906. return error;
  1907. }
  1908. static long do_rmdir(int dfd, const char __user *pathname)
  1909. {
  1910. int error = 0;
  1911. char * name;
  1912. struct dentry *dentry;
  1913. struct nameidata nd;
  1914. error = user_path_parent(dfd, pathname, &nd, &name);
  1915. if (error)
  1916. return error;
  1917. switch(nd.last_type) {
  1918. case LAST_DOTDOT:
  1919. error = -ENOTEMPTY;
  1920. goto exit1;
  1921. case LAST_DOT:
  1922. error = -EINVAL;
  1923. goto exit1;
  1924. case LAST_ROOT:
  1925. error = -EBUSY;
  1926. goto exit1;
  1927. }
  1928. nd.flags &= ~LOOKUP_PARENT;
  1929. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1930. dentry = lookup_hash(&nd);
  1931. error = PTR_ERR(dentry);
  1932. if (IS_ERR(dentry))
  1933. goto exit2;
  1934. error = mnt_want_write(nd.path.mnt);
  1935. if (error)
  1936. goto exit3;
  1937. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  1938. mnt_drop_write(nd.path.mnt);
  1939. exit3:
  1940. dput(dentry);
  1941. exit2:
  1942. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1943. exit1:
  1944. path_put(&nd.path);
  1945. putname(name);
  1946. return error;
  1947. }
  1948. asmlinkage long sys_rmdir(const char __user *pathname)
  1949. {
  1950. return do_rmdir(AT_FDCWD, pathname);
  1951. }
  1952. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1953. {
  1954. int error = may_delete(dir, dentry, 0);
  1955. if (error)
  1956. return error;
  1957. if (!dir->i_op || !dir->i_op->unlink)
  1958. return -EPERM;
  1959. DQUOT_INIT(dir);
  1960. mutex_lock(&dentry->d_inode->i_mutex);
  1961. if (d_mountpoint(dentry))
  1962. error = -EBUSY;
  1963. else {
  1964. error = security_inode_unlink(dir, dentry);
  1965. if (!error)
  1966. error = dir->i_op->unlink(dir, dentry);
  1967. }
  1968. mutex_unlock(&dentry->d_inode->i_mutex);
  1969. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1970. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1971. fsnotify_link_count(dentry->d_inode);
  1972. d_delete(dentry);
  1973. }
  1974. return error;
  1975. }
  1976. /*
  1977. * Make sure that the actual truncation of the file will occur outside its
  1978. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1979. * writeout happening, and we don't want to prevent access to the directory
  1980. * while waiting on the I/O.
  1981. */
  1982. static long do_unlinkat(int dfd, const char __user *pathname)
  1983. {
  1984. int error;
  1985. char *name;
  1986. struct dentry *dentry;
  1987. struct nameidata nd;
  1988. struct inode *inode = NULL;
  1989. error = user_path_parent(dfd, pathname, &nd, &name);
  1990. if (error)
  1991. return error;
  1992. error = -EISDIR;
  1993. if (nd.last_type != LAST_NORM)
  1994. goto exit1;
  1995. nd.flags &= ~LOOKUP_PARENT;
  1996. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1997. dentry = lookup_hash(&nd);
  1998. error = PTR_ERR(dentry);
  1999. if (!IS_ERR(dentry)) {
  2000. /* Why not before? Because we want correct error value */
  2001. if (nd.last.name[nd.last.len])
  2002. goto slashes;
  2003. inode = dentry->d_inode;
  2004. if (inode)
  2005. atomic_inc(&inode->i_count);
  2006. error = mnt_want_write(nd.path.mnt);
  2007. if (error)
  2008. goto exit2;
  2009. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2010. mnt_drop_write(nd.path.mnt);
  2011. exit2:
  2012. dput(dentry);
  2013. }
  2014. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2015. if (inode)
  2016. iput(inode); /* truncate the inode here */
  2017. exit1:
  2018. path_put(&nd.path);
  2019. putname(name);
  2020. return error;
  2021. slashes:
  2022. error = !dentry->d_inode ? -ENOENT :
  2023. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2024. goto exit2;
  2025. }
  2026. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  2027. {
  2028. if ((flag & ~AT_REMOVEDIR) != 0)
  2029. return -EINVAL;
  2030. if (flag & AT_REMOVEDIR)
  2031. return do_rmdir(dfd, pathname);
  2032. return do_unlinkat(dfd, pathname);
  2033. }
  2034. asmlinkage long sys_unlink(const char __user *pathname)
  2035. {
  2036. return do_unlinkat(AT_FDCWD, pathname);
  2037. }
  2038. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2039. {
  2040. int error = may_create(dir, dentry);
  2041. if (error)
  2042. return error;
  2043. if (!dir->i_op || !dir->i_op->symlink)
  2044. return -EPERM;
  2045. error = security_inode_symlink(dir, dentry, oldname);
  2046. if (error)
  2047. return error;
  2048. DQUOT_INIT(dir);
  2049. error = dir->i_op->symlink(dir, dentry, oldname);
  2050. if (!error)
  2051. fsnotify_create(dir, dentry);
  2052. return error;
  2053. }
  2054. asmlinkage long sys_symlinkat(const char __user *oldname,
  2055. int newdfd, const char __user *newname)
  2056. {
  2057. int error;
  2058. char *from;
  2059. char *to;
  2060. struct dentry *dentry;
  2061. struct nameidata nd;
  2062. from = getname(oldname);
  2063. if (IS_ERR(from))
  2064. return PTR_ERR(from);
  2065. error = user_path_parent(newdfd, newname, &nd, &to);
  2066. if (error)
  2067. goto out_putname;
  2068. dentry = lookup_create(&nd, 0);
  2069. error = PTR_ERR(dentry);
  2070. if (IS_ERR(dentry))
  2071. goto out_unlock;
  2072. error = mnt_want_write(nd.path.mnt);
  2073. if (error)
  2074. goto out_dput;
  2075. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2076. mnt_drop_write(nd.path.mnt);
  2077. out_dput:
  2078. dput(dentry);
  2079. out_unlock:
  2080. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2081. path_put(&nd.path);
  2082. putname(to);
  2083. out_putname:
  2084. putname(from);
  2085. return error;
  2086. }
  2087. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  2088. {
  2089. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2090. }
  2091. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2092. {
  2093. struct inode *inode = old_dentry->d_inode;
  2094. int error;
  2095. if (!inode)
  2096. return -ENOENT;
  2097. error = may_create(dir, new_dentry);
  2098. if (error)
  2099. return error;
  2100. if (dir->i_sb != inode->i_sb)
  2101. return -EXDEV;
  2102. /*
  2103. * A link to an append-only or immutable file cannot be created.
  2104. */
  2105. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2106. return -EPERM;
  2107. if (!dir->i_op || !dir->i_op->link)
  2108. return -EPERM;
  2109. if (S_ISDIR(inode->i_mode))
  2110. return -EPERM;
  2111. error = security_inode_link(old_dentry, dir, new_dentry);
  2112. if (error)
  2113. return error;
  2114. mutex_lock(&inode->i_mutex);
  2115. DQUOT_INIT(dir);
  2116. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2117. mutex_unlock(&inode->i_mutex);
  2118. if (!error)
  2119. fsnotify_link(dir, inode, new_dentry);
  2120. return error;
  2121. }
  2122. /*
  2123. * Hardlinks are often used in delicate situations. We avoid
  2124. * security-related surprises by not following symlinks on the
  2125. * newname. --KAB
  2126. *
  2127. * We don't follow them on the oldname either to be compatible
  2128. * with linux 2.0, and to avoid hard-linking to directories
  2129. * and other special files. --ADM
  2130. */
  2131. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  2132. int newdfd, const char __user *newname,
  2133. int flags)
  2134. {
  2135. struct dentry *new_dentry;
  2136. struct nameidata nd;
  2137. struct path old_path;
  2138. int error;
  2139. char *to;
  2140. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2141. return -EINVAL;
  2142. error = user_path_at(olddfd, oldname,
  2143. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2144. &old_path);
  2145. if (error)
  2146. return error;
  2147. error = user_path_parent(newdfd, newname, &nd, &to);
  2148. if (error)
  2149. goto out;
  2150. error = -EXDEV;
  2151. if (old_path.mnt != nd.path.mnt)
  2152. goto out_release;
  2153. new_dentry = lookup_create(&nd, 0);
  2154. error = PTR_ERR(new_dentry);
  2155. if (IS_ERR(new_dentry))
  2156. goto out_unlock;
  2157. error = mnt_want_write(nd.path.mnt);
  2158. if (error)
  2159. goto out_dput;
  2160. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2161. mnt_drop_write(nd.path.mnt);
  2162. out_dput:
  2163. dput(new_dentry);
  2164. out_unlock:
  2165. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2166. out_release:
  2167. path_put(&nd.path);
  2168. putname(to);
  2169. out:
  2170. path_put(&old_path);
  2171. return error;
  2172. }
  2173. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2174. {
  2175. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2176. }
  2177. /*
  2178. * The worst of all namespace operations - renaming directory. "Perverted"
  2179. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2180. * Problems:
  2181. * a) we can get into loop creation. Check is done in is_subdir().
  2182. * b) race potential - two innocent renames can create a loop together.
  2183. * That's where 4.4 screws up. Current fix: serialization on
  2184. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2185. * story.
  2186. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2187. * And that - after we got ->i_mutex on parents (until then we don't know
  2188. * whether the target exists). Solution: try to be smart with locking
  2189. * order for inodes. We rely on the fact that tree topology may change
  2190. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2191. * move will be locked. Thus we can rank directories by the tree
  2192. * (ancestors first) and rank all non-directories after them.
  2193. * That works since everybody except rename does "lock parent, lookup,
  2194. * lock child" and rename is under ->s_vfs_rename_mutex.
  2195. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2196. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2197. * we'd better make sure that there's no link(2) for them.
  2198. * d) some filesystems don't support opened-but-unlinked directories,
  2199. * either because of layout or because they are not ready to deal with
  2200. * all cases correctly. The latter will be fixed (taking this sort of
  2201. * stuff into VFS), but the former is not going away. Solution: the same
  2202. * trick as in rmdir().
  2203. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2204. * we are removing the target. Solution: we will have to grab ->i_mutex
  2205. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2206. * ->i_mutex on parents, which works but leads to some truely excessive
  2207. * locking].
  2208. */
  2209. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2210. struct inode *new_dir, struct dentry *new_dentry)
  2211. {
  2212. int error = 0;
  2213. struct inode *target;
  2214. /*
  2215. * If we are going to change the parent - check write permissions,
  2216. * we'll need to flip '..'.
  2217. */
  2218. if (new_dir != old_dir) {
  2219. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2220. if (error)
  2221. return error;
  2222. }
  2223. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2224. if (error)
  2225. return error;
  2226. target = new_dentry->d_inode;
  2227. if (target) {
  2228. mutex_lock(&target->i_mutex);
  2229. dentry_unhash(new_dentry);
  2230. }
  2231. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2232. error = -EBUSY;
  2233. else
  2234. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2235. if (target) {
  2236. if (!error)
  2237. target->i_flags |= S_DEAD;
  2238. mutex_unlock(&target->i_mutex);
  2239. if (d_unhashed(new_dentry))
  2240. d_rehash(new_dentry);
  2241. dput(new_dentry);
  2242. }
  2243. if (!error)
  2244. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2245. d_move(old_dentry,new_dentry);
  2246. return error;
  2247. }
  2248. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2249. struct inode *new_dir, struct dentry *new_dentry)
  2250. {
  2251. struct inode *target;
  2252. int error;
  2253. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2254. if (error)
  2255. return error;
  2256. dget(new_dentry);
  2257. target = new_dentry->d_inode;
  2258. if (target)
  2259. mutex_lock(&target->i_mutex);
  2260. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2261. error = -EBUSY;
  2262. else
  2263. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2264. if (!error) {
  2265. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2266. d_move(old_dentry, new_dentry);
  2267. }
  2268. if (target)
  2269. mutex_unlock(&target->i_mutex);
  2270. dput(new_dentry);
  2271. return error;
  2272. }
  2273. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2274. struct inode *new_dir, struct dentry *new_dentry)
  2275. {
  2276. int error;
  2277. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2278. const char *old_name;
  2279. if (old_dentry->d_inode == new_dentry->d_inode)
  2280. return 0;
  2281. error = may_delete(old_dir, old_dentry, is_dir);
  2282. if (error)
  2283. return error;
  2284. if (!new_dentry->d_inode)
  2285. error = may_create(new_dir, new_dentry);
  2286. else
  2287. error = may_delete(new_dir, new_dentry, is_dir);
  2288. if (error)
  2289. return error;
  2290. if (!old_dir->i_op || !old_dir->i_op->rename)
  2291. return -EPERM;
  2292. DQUOT_INIT(old_dir);
  2293. DQUOT_INIT(new_dir);
  2294. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2295. if (is_dir)
  2296. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2297. else
  2298. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2299. if (!error) {
  2300. const char *new_name = old_dentry->d_name.name;
  2301. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2302. new_dentry->d_inode, old_dentry);
  2303. }
  2304. fsnotify_oldname_free(old_name);
  2305. return error;
  2306. }
  2307. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2308. int newdfd, const char __user *newname)
  2309. {
  2310. struct dentry *old_dir, *new_dir;
  2311. struct dentry *old_dentry, *new_dentry;
  2312. struct dentry *trap;
  2313. struct nameidata oldnd, newnd;
  2314. char *from;
  2315. char *to;
  2316. int error;
  2317. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2318. if (error)
  2319. goto exit;
  2320. error = user_path_parent(newdfd, newname, &newnd, &to);
  2321. if (error)
  2322. goto exit1;
  2323. error = -EXDEV;
  2324. if (oldnd.path.mnt != newnd.path.mnt)
  2325. goto exit2;
  2326. old_dir = oldnd.path.dentry;
  2327. error = -EBUSY;
  2328. if (oldnd.last_type != LAST_NORM)
  2329. goto exit2;
  2330. new_dir = newnd.path.dentry;
  2331. if (newnd.last_type != LAST_NORM)
  2332. goto exit2;
  2333. oldnd.flags &= ~LOOKUP_PARENT;
  2334. newnd.flags &= ~LOOKUP_PARENT;
  2335. newnd.flags |= LOOKUP_RENAME_TARGET;
  2336. trap = lock_rename(new_dir, old_dir);
  2337. old_dentry = lookup_hash(&oldnd);
  2338. error = PTR_ERR(old_dentry);
  2339. if (IS_ERR(old_dentry))
  2340. goto exit3;
  2341. /* source must exist */
  2342. error = -ENOENT;
  2343. if (!old_dentry->d_inode)
  2344. goto exit4;
  2345. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2346. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2347. error = -ENOTDIR;
  2348. if (oldnd.last.name[oldnd.last.len])
  2349. goto exit4;
  2350. if (newnd.last.name[newnd.last.len])
  2351. goto exit4;
  2352. }
  2353. /* source should not be ancestor of target */
  2354. error = -EINVAL;
  2355. if (old_dentry == trap)
  2356. goto exit4;
  2357. new_dentry = lookup_hash(&newnd);
  2358. error = PTR_ERR(new_dentry);
  2359. if (IS_ERR(new_dentry))
  2360. goto exit4;
  2361. /* target should not be an ancestor of source */
  2362. error = -ENOTEMPTY;
  2363. if (new_dentry == trap)
  2364. goto exit5;
  2365. error = mnt_want_write(oldnd.path.mnt);
  2366. if (error)
  2367. goto exit5;
  2368. error = vfs_rename(old_dir->d_inode, old_dentry,
  2369. new_dir->d_inode, new_dentry);
  2370. mnt_drop_write(oldnd.path.mnt);
  2371. exit5:
  2372. dput(new_dentry);
  2373. exit4:
  2374. dput(old_dentry);
  2375. exit3:
  2376. unlock_rename(new_dir, old_dir);
  2377. exit2:
  2378. path_put(&newnd.path);
  2379. putname(to);
  2380. exit1:
  2381. path_put(&oldnd.path);
  2382. putname(from);
  2383. exit:
  2384. return error;
  2385. }
  2386. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2387. {
  2388. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2389. }
  2390. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2391. {
  2392. int len;
  2393. len = PTR_ERR(link);
  2394. if (IS_ERR(link))
  2395. goto out;
  2396. len = strlen(link);
  2397. if (len > (unsigned) buflen)
  2398. len = buflen;
  2399. if (copy_to_user(buffer, link, len))
  2400. len = -EFAULT;
  2401. out:
  2402. return len;
  2403. }
  2404. /*
  2405. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2406. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2407. * using) it for any given inode is up to filesystem.
  2408. */
  2409. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2410. {
  2411. struct nameidata nd;
  2412. void *cookie;
  2413. int res;
  2414. nd.depth = 0;
  2415. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2416. if (IS_ERR(cookie))
  2417. return PTR_ERR(cookie);
  2418. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2419. if (dentry->d_inode->i_op->put_link)
  2420. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2421. return res;
  2422. }
  2423. int vfs_follow_link(struct nameidata *nd, const char *link)
  2424. {
  2425. return __vfs_follow_link(nd, link);
  2426. }
  2427. /* get the link contents into pagecache */
  2428. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2429. {
  2430. struct page * page;
  2431. struct address_space *mapping = dentry->d_inode->i_mapping;
  2432. page = read_mapping_page(mapping, 0, NULL);
  2433. if (IS_ERR(page))
  2434. return (char*)page;
  2435. *ppage = page;
  2436. return kmap(page);
  2437. }
  2438. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2439. {
  2440. struct page *page = NULL;
  2441. char *s = page_getlink(dentry, &page);
  2442. int res = vfs_readlink(dentry,buffer,buflen,s);
  2443. if (page) {
  2444. kunmap(page);
  2445. page_cache_release(page);
  2446. }
  2447. return res;
  2448. }
  2449. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2450. {
  2451. struct page *page = NULL;
  2452. nd_set_link(nd, page_getlink(dentry, &page));
  2453. return page;
  2454. }
  2455. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2456. {
  2457. struct page *page = cookie;
  2458. if (page) {
  2459. kunmap(page);
  2460. page_cache_release(page);
  2461. }
  2462. }
  2463. int __page_symlink(struct inode *inode, const char *symname, int len,
  2464. gfp_t gfp_mask)
  2465. {
  2466. struct address_space *mapping = inode->i_mapping;
  2467. struct page *page;
  2468. void *fsdata;
  2469. int err;
  2470. char *kaddr;
  2471. retry:
  2472. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2473. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  2474. if (err)
  2475. goto fail;
  2476. kaddr = kmap_atomic(page, KM_USER0);
  2477. memcpy(kaddr, symname, len-1);
  2478. kunmap_atomic(kaddr, KM_USER0);
  2479. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2480. page, fsdata);
  2481. if (err < 0)
  2482. goto fail;
  2483. if (err < len-1)
  2484. goto retry;
  2485. mark_inode_dirty(inode);
  2486. return 0;
  2487. fail:
  2488. return err;
  2489. }
  2490. int page_symlink(struct inode *inode, const char *symname, int len)
  2491. {
  2492. return __page_symlink(inode, symname, len,
  2493. mapping_gfp_mask(inode->i_mapping));
  2494. }
  2495. const struct inode_operations page_symlink_inode_operations = {
  2496. .readlink = generic_readlink,
  2497. .follow_link = page_follow_link_light,
  2498. .put_link = page_put_link,
  2499. };
  2500. EXPORT_SYMBOL(user_path_at);
  2501. EXPORT_SYMBOL(follow_down);
  2502. EXPORT_SYMBOL(follow_up);
  2503. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2504. EXPORT_SYMBOL(getname);
  2505. EXPORT_SYMBOL(lock_rename);
  2506. EXPORT_SYMBOL(lookup_one_len);
  2507. EXPORT_SYMBOL(page_follow_link_light);
  2508. EXPORT_SYMBOL(page_put_link);
  2509. EXPORT_SYMBOL(page_readlink);
  2510. EXPORT_SYMBOL(__page_symlink);
  2511. EXPORT_SYMBOL(page_symlink);
  2512. EXPORT_SYMBOL(page_symlink_inode_operations);
  2513. EXPORT_SYMBOL(path_lookup);
  2514. EXPORT_SYMBOL(kern_path);
  2515. EXPORT_SYMBOL(vfs_path_lookup);
  2516. EXPORT_SYMBOL(inode_permission);
  2517. EXPORT_SYMBOL(vfs_permission);
  2518. EXPORT_SYMBOL(file_permission);
  2519. EXPORT_SYMBOL(unlock_rename);
  2520. EXPORT_SYMBOL(vfs_create);
  2521. EXPORT_SYMBOL(vfs_follow_link);
  2522. EXPORT_SYMBOL(vfs_link);
  2523. EXPORT_SYMBOL(vfs_mkdir);
  2524. EXPORT_SYMBOL(vfs_mknod);
  2525. EXPORT_SYMBOL(generic_permission);
  2526. EXPORT_SYMBOL(vfs_readlink);
  2527. EXPORT_SYMBOL(vfs_rename);
  2528. EXPORT_SYMBOL(vfs_rmdir);
  2529. EXPORT_SYMBOL(vfs_symlink);
  2530. EXPORT_SYMBOL(vfs_unlink);
  2531. EXPORT_SYMBOL(dentry_unhash);
  2532. EXPORT_SYMBOL(generic_readlink);