namei.c 71 KB

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