namei.c 60 KB

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