namei.c 70 KB

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