namei.c 73 KB

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