namespace.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/acct.h>
  17. #include <linux/capability.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/module.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/mnt_namespace.h>
  23. #include <linux/namei.h>
  24. #include <linux/nsproxy.h>
  25. #include <linux/security.h>
  26. #include <linux/mount.h>
  27. #include <linux/ramfs.h>
  28. #include <linux/log2.h>
  29. #include <linux/idr.h>
  30. #include <linux/fs_struct.h>
  31. #include <linux/fsnotify.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/unistd.h>
  34. #include "pnode.h"
  35. #include "internal.h"
  36. #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
  37. #define HASH_SIZE (1UL << HASH_SHIFT)
  38. /* spinlock for vfsmount related operations, inplace of dcache_lock */
  39. __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
  40. static int event;
  41. static DEFINE_IDA(mnt_id_ida);
  42. static DEFINE_IDA(mnt_group_ida);
  43. static int mnt_id_start = 0;
  44. static int mnt_group_start = 1;
  45. static struct list_head *mount_hashtable __read_mostly;
  46. static struct kmem_cache *mnt_cache __read_mostly;
  47. static struct rw_semaphore namespace_sem;
  48. /* /sys/fs */
  49. struct kobject *fs_kobj;
  50. EXPORT_SYMBOL_GPL(fs_kobj);
  51. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  52. {
  53. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  54. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  55. tmp = tmp + (tmp >> HASH_SHIFT);
  56. return tmp & (HASH_SIZE - 1);
  57. }
  58. #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
  59. /* allocation is serialized by namespace_sem */
  60. static int mnt_alloc_id(struct vfsmount *mnt)
  61. {
  62. int res;
  63. retry:
  64. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  65. spin_lock(&vfsmount_lock);
  66. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  67. if (!res)
  68. mnt_id_start = mnt->mnt_id + 1;
  69. spin_unlock(&vfsmount_lock);
  70. if (res == -EAGAIN)
  71. goto retry;
  72. return res;
  73. }
  74. static void mnt_free_id(struct vfsmount *mnt)
  75. {
  76. int id = mnt->mnt_id;
  77. spin_lock(&vfsmount_lock);
  78. ida_remove(&mnt_id_ida, id);
  79. if (mnt_id_start > id)
  80. mnt_id_start = id;
  81. spin_unlock(&vfsmount_lock);
  82. }
  83. /*
  84. * Allocate a new peer group ID
  85. *
  86. * mnt_group_ida is protected by namespace_sem
  87. */
  88. static int mnt_alloc_group_id(struct vfsmount *mnt)
  89. {
  90. int res;
  91. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  92. return -ENOMEM;
  93. res = ida_get_new_above(&mnt_group_ida,
  94. mnt_group_start,
  95. &mnt->mnt_group_id);
  96. if (!res)
  97. mnt_group_start = mnt->mnt_group_id + 1;
  98. return res;
  99. }
  100. /*
  101. * Release a peer group ID
  102. */
  103. void mnt_release_group_id(struct vfsmount *mnt)
  104. {
  105. int id = mnt->mnt_group_id;
  106. ida_remove(&mnt_group_ida, id);
  107. if (mnt_group_start > id)
  108. mnt_group_start = id;
  109. mnt->mnt_group_id = 0;
  110. }
  111. struct vfsmount *alloc_vfsmnt(const char *name)
  112. {
  113. struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  114. if (mnt) {
  115. int err;
  116. err = mnt_alloc_id(mnt);
  117. if (err)
  118. goto out_free_cache;
  119. if (name) {
  120. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  121. if (!mnt->mnt_devname)
  122. goto out_free_id;
  123. }
  124. atomic_set(&mnt->mnt_count, 1);
  125. INIT_LIST_HEAD(&mnt->mnt_hash);
  126. INIT_LIST_HEAD(&mnt->mnt_child);
  127. INIT_LIST_HEAD(&mnt->mnt_mounts);
  128. INIT_LIST_HEAD(&mnt->mnt_list);
  129. INIT_LIST_HEAD(&mnt->mnt_expire);
  130. INIT_LIST_HEAD(&mnt->mnt_share);
  131. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  132. INIT_LIST_HEAD(&mnt->mnt_slave);
  133. #ifdef CONFIG_FSNOTIFY
  134. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  135. #endif
  136. #ifdef CONFIG_SMP
  137. mnt->mnt_writers = alloc_percpu(int);
  138. if (!mnt->mnt_writers)
  139. goto out_free_devname;
  140. #else
  141. mnt->mnt_writers = 0;
  142. #endif
  143. }
  144. return mnt;
  145. #ifdef CONFIG_SMP
  146. out_free_devname:
  147. kfree(mnt->mnt_devname);
  148. #endif
  149. out_free_id:
  150. mnt_free_id(mnt);
  151. out_free_cache:
  152. kmem_cache_free(mnt_cache, mnt);
  153. return NULL;
  154. }
  155. /*
  156. * Most r/o checks on a fs are for operations that take
  157. * discrete amounts of time, like a write() or unlink().
  158. * We must keep track of when those operations start
  159. * (for permission checks) and when they end, so that
  160. * we can determine when writes are able to occur to
  161. * a filesystem.
  162. */
  163. /*
  164. * __mnt_is_readonly: check whether a mount is read-only
  165. * @mnt: the mount to check for its write status
  166. *
  167. * This shouldn't be used directly ouside of the VFS.
  168. * It does not guarantee that the filesystem will stay
  169. * r/w, just that it is right *now*. This can not and
  170. * should not be used in place of IS_RDONLY(inode).
  171. * mnt_want/drop_write() will _keep_ the filesystem
  172. * r/w.
  173. */
  174. int __mnt_is_readonly(struct vfsmount *mnt)
  175. {
  176. if (mnt->mnt_flags & MNT_READONLY)
  177. return 1;
  178. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  179. return 1;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  183. static inline void inc_mnt_writers(struct vfsmount *mnt)
  184. {
  185. #ifdef CONFIG_SMP
  186. (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++;
  187. #else
  188. mnt->mnt_writers++;
  189. #endif
  190. }
  191. static inline void dec_mnt_writers(struct vfsmount *mnt)
  192. {
  193. #ifdef CONFIG_SMP
  194. (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--;
  195. #else
  196. mnt->mnt_writers--;
  197. #endif
  198. }
  199. static unsigned int count_mnt_writers(struct vfsmount *mnt)
  200. {
  201. #ifdef CONFIG_SMP
  202. unsigned int count = 0;
  203. int cpu;
  204. for_each_possible_cpu(cpu) {
  205. count += *per_cpu_ptr(mnt->mnt_writers, cpu);
  206. }
  207. return count;
  208. #else
  209. return mnt->mnt_writers;
  210. #endif
  211. }
  212. /*
  213. * Most r/o checks on a fs are for operations that take
  214. * discrete amounts of time, like a write() or unlink().
  215. * We must keep track of when those operations start
  216. * (for permission checks) and when they end, so that
  217. * we can determine when writes are able to occur to
  218. * a filesystem.
  219. */
  220. /**
  221. * mnt_want_write - get write access to a mount
  222. * @mnt: the mount on which to take a write
  223. *
  224. * This tells the low-level filesystem that a write is
  225. * about to be performed to it, and makes sure that
  226. * writes are allowed before returning success. When
  227. * the write operation is finished, mnt_drop_write()
  228. * must be called. This is effectively a refcount.
  229. */
  230. int mnt_want_write(struct vfsmount *mnt)
  231. {
  232. int ret = 0;
  233. preempt_disable();
  234. inc_mnt_writers(mnt);
  235. /*
  236. * The store to inc_mnt_writers must be visible before we pass
  237. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  238. * incremented count after it has set MNT_WRITE_HOLD.
  239. */
  240. smp_mb();
  241. while (mnt->mnt_flags & MNT_WRITE_HOLD)
  242. cpu_relax();
  243. /*
  244. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  245. * be set to match its requirements. So we must not load that until
  246. * MNT_WRITE_HOLD is cleared.
  247. */
  248. smp_rmb();
  249. if (__mnt_is_readonly(mnt)) {
  250. dec_mnt_writers(mnt);
  251. ret = -EROFS;
  252. goto out;
  253. }
  254. out:
  255. preempt_enable();
  256. return ret;
  257. }
  258. EXPORT_SYMBOL_GPL(mnt_want_write);
  259. /**
  260. * mnt_clone_write - get write access to a mount
  261. * @mnt: the mount on which to take a write
  262. *
  263. * This is effectively like mnt_want_write, except
  264. * it must only be used to take an extra write reference
  265. * on a mountpoint that we already know has a write reference
  266. * on it. This allows some optimisation.
  267. *
  268. * After finished, mnt_drop_write must be called as usual to
  269. * drop the reference.
  270. */
  271. int mnt_clone_write(struct vfsmount *mnt)
  272. {
  273. /* superblock may be r/o */
  274. if (__mnt_is_readonly(mnt))
  275. return -EROFS;
  276. preempt_disable();
  277. inc_mnt_writers(mnt);
  278. preempt_enable();
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(mnt_clone_write);
  282. /**
  283. * mnt_want_write_file - get write access to a file's mount
  284. * @file: the file who's mount on which to take a write
  285. *
  286. * This is like mnt_want_write, but it takes a file and can
  287. * do some optimisations if the file is open for write already
  288. */
  289. int mnt_want_write_file(struct file *file)
  290. {
  291. struct inode *inode = file->f_dentry->d_inode;
  292. if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
  293. return mnt_want_write(file->f_path.mnt);
  294. else
  295. return mnt_clone_write(file->f_path.mnt);
  296. }
  297. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  298. /**
  299. * mnt_drop_write - give up write access to a mount
  300. * @mnt: the mount on which to give up write access
  301. *
  302. * Tells the low-level filesystem that we are done
  303. * performing writes to it. Must be matched with
  304. * mnt_want_write() call above.
  305. */
  306. void mnt_drop_write(struct vfsmount *mnt)
  307. {
  308. preempt_disable();
  309. dec_mnt_writers(mnt);
  310. preempt_enable();
  311. }
  312. EXPORT_SYMBOL_GPL(mnt_drop_write);
  313. static int mnt_make_readonly(struct vfsmount *mnt)
  314. {
  315. int ret = 0;
  316. spin_lock(&vfsmount_lock);
  317. mnt->mnt_flags |= MNT_WRITE_HOLD;
  318. /*
  319. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  320. * should be visible before we do.
  321. */
  322. smp_mb();
  323. /*
  324. * With writers on hold, if this value is zero, then there are
  325. * definitely no active writers (although held writers may subsequently
  326. * increment the count, they'll have to wait, and decrement it after
  327. * seeing MNT_READONLY).
  328. *
  329. * It is OK to have counter incremented on one CPU and decremented on
  330. * another: the sum will add up correctly. The danger would be when we
  331. * sum up each counter, if we read a counter before it is incremented,
  332. * but then read another CPU's count which it has been subsequently
  333. * decremented from -- we would see more decrements than we should.
  334. * MNT_WRITE_HOLD protects against this scenario, because
  335. * mnt_want_write first increments count, then smp_mb, then spins on
  336. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  337. * we're counting up here.
  338. */
  339. if (count_mnt_writers(mnt) > 0)
  340. ret = -EBUSY;
  341. else
  342. mnt->mnt_flags |= MNT_READONLY;
  343. /*
  344. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  345. * that become unheld will see MNT_READONLY.
  346. */
  347. smp_wmb();
  348. mnt->mnt_flags &= ~MNT_WRITE_HOLD;
  349. spin_unlock(&vfsmount_lock);
  350. return ret;
  351. }
  352. static void __mnt_unmake_readonly(struct vfsmount *mnt)
  353. {
  354. spin_lock(&vfsmount_lock);
  355. mnt->mnt_flags &= ~MNT_READONLY;
  356. spin_unlock(&vfsmount_lock);
  357. }
  358. void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
  359. {
  360. mnt->mnt_sb = sb;
  361. mnt->mnt_root = dget(sb->s_root);
  362. }
  363. EXPORT_SYMBOL(simple_set_mnt);
  364. void free_vfsmnt(struct vfsmount *mnt)
  365. {
  366. kfree(mnt->mnt_devname);
  367. mnt_free_id(mnt);
  368. #ifdef CONFIG_SMP
  369. free_percpu(mnt->mnt_writers);
  370. #endif
  371. kmem_cache_free(mnt_cache, mnt);
  372. }
  373. /*
  374. * find the first or last mount at @dentry on vfsmount @mnt depending on
  375. * @dir. If @dir is set return the first mount else return the last mount.
  376. */
  377. struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  378. int dir)
  379. {
  380. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  381. struct list_head *tmp = head;
  382. struct vfsmount *p, *found = NULL;
  383. for (;;) {
  384. tmp = dir ? tmp->next : tmp->prev;
  385. p = NULL;
  386. if (tmp == head)
  387. break;
  388. p = list_entry(tmp, struct vfsmount, mnt_hash);
  389. if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
  390. found = p;
  391. break;
  392. }
  393. }
  394. return found;
  395. }
  396. /*
  397. * lookup_mnt increments the ref count before returning
  398. * the vfsmount struct.
  399. */
  400. struct vfsmount *lookup_mnt(struct path *path)
  401. {
  402. struct vfsmount *child_mnt;
  403. spin_lock(&vfsmount_lock);
  404. if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
  405. mntget(child_mnt);
  406. spin_unlock(&vfsmount_lock);
  407. return child_mnt;
  408. }
  409. static inline int check_mnt(struct vfsmount *mnt)
  410. {
  411. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  412. }
  413. static void touch_mnt_namespace(struct mnt_namespace *ns)
  414. {
  415. if (ns) {
  416. ns->event = ++event;
  417. wake_up_interruptible(&ns->poll);
  418. }
  419. }
  420. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  421. {
  422. if (ns && ns->event != event) {
  423. ns->event = event;
  424. wake_up_interruptible(&ns->poll);
  425. }
  426. }
  427. static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
  428. {
  429. old_path->dentry = mnt->mnt_mountpoint;
  430. old_path->mnt = mnt->mnt_parent;
  431. mnt->mnt_parent = mnt;
  432. mnt->mnt_mountpoint = mnt->mnt_root;
  433. list_del_init(&mnt->mnt_child);
  434. list_del_init(&mnt->mnt_hash);
  435. old_path->dentry->d_mounted--;
  436. }
  437. void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
  438. struct vfsmount *child_mnt)
  439. {
  440. child_mnt->mnt_parent = mntget(mnt);
  441. child_mnt->mnt_mountpoint = dget(dentry);
  442. dentry->d_mounted++;
  443. }
  444. static void attach_mnt(struct vfsmount *mnt, struct path *path)
  445. {
  446. mnt_set_mountpoint(path->mnt, path->dentry, mnt);
  447. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  448. hash(path->mnt, path->dentry));
  449. list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
  450. }
  451. /*
  452. * the caller must hold vfsmount_lock
  453. */
  454. static void commit_tree(struct vfsmount *mnt)
  455. {
  456. struct vfsmount *parent = mnt->mnt_parent;
  457. struct vfsmount *m;
  458. LIST_HEAD(head);
  459. struct mnt_namespace *n = parent->mnt_ns;
  460. BUG_ON(parent == mnt);
  461. list_add_tail(&head, &mnt->mnt_list);
  462. list_for_each_entry(m, &head, mnt_list)
  463. m->mnt_ns = n;
  464. list_splice(&head, n->list.prev);
  465. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  466. hash(parent, mnt->mnt_mountpoint));
  467. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  468. touch_mnt_namespace(n);
  469. }
  470. static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
  471. {
  472. struct list_head *next = p->mnt_mounts.next;
  473. if (next == &p->mnt_mounts) {
  474. while (1) {
  475. if (p == root)
  476. return NULL;
  477. next = p->mnt_child.next;
  478. if (next != &p->mnt_parent->mnt_mounts)
  479. break;
  480. p = p->mnt_parent;
  481. }
  482. }
  483. return list_entry(next, struct vfsmount, mnt_child);
  484. }
  485. static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
  486. {
  487. struct list_head *prev = p->mnt_mounts.prev;
  488. while (prev != &p->mnt_mounts) {
  489. p = list_entry(prev, struct vfsmount, mnt_child);
  490. prev = p->mnt_mounts.prev;
  491. }
  492. return p;
  493. }
  494. static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
  495. int flag)
  496. {
  497. struct super_block *sb = old->mnt_sb;
  498. struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
  499. if (mnt) {
  500. if (flag & (CL_SLAVE | CL_PRIVATE))
  501. mnt->mnt_group_id = 0; /* not a peer of original */
  502. else
  503. mnt->mnt_group_id = old->mnt_group_id;
  504. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  505. int err = mnt_alloc_group_id(mnt);
  506. if (err)
  507. goto out_free;
  508. }
  509. mnt->mnt_flags = old->mnt_flags;
  510. atomic_inc(&sb->s_active);
  511. mnt->mnt_sb = sb;
  512. mnt->mnt_root = dget(root);
  513. mnt->mnt_mountpoint = mnt->mnt_root;
  514. mnt->mnt_parent = mnt;
  515. if (flag & CL_SLAVE) {
  516. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  517. mnt->mnt_master = old;
  518. CLEAR_MNT_SHARED(mnt);
  519. } else if (!(flag & CL_PRIVATE)) {
  520. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  521. list_add(&mnt->mnt_share, &old->mnt_share);
  522. if (IS_MNT_SLAVE(old))
  523. list_add(&mnt->mnt_slave, &old->mnt_slave);
  524. mnt->mnt_master = old->mnt_master;
  525. }
  526. if (flag & CL_MAKE_SHARED)
  527. set_mnt_shared(mnt);
  528. /* stick the duplicate mount on the same expiry list
  529. * as the original if that was on one */
  530. if (flag & CL_EXPIRE) {
  531. if (!list_empty(&old->mnt_expire))
  532. list_add(&mnt->mnt_expire, &old->mnt_expire);
  533. }
  534. }
  535. return mnt;
  536. out_free:
  537. free_vfsmnt(mnt);
  538. return NULL;
  539. }
  540. static inline void __mntput(struct vfsmount *mnt)
  541. {
  542. struct super_block *sb = mnt->mnt_sb;
  543. /*
  544. * This probably indicates that somebody messed
  545. * up a mnt_want/drop_write() pair. If this
  546. * happens, the filesystem was probably unable
  547. * to make r/w->r/o transitions.
  548. */
  549. /*
  550. * atomic_dec_and_lock() used to deal with ->mnt_count decrements
  551. * provides barriers, so count_mnt_writers() below is safe. AV
  552. */
  553. WARN_ON(count_mnt_writers(mnt));
  554. fsnotify_vfsmount_delete(mnt);
  555. dput(mnt->mnt_root);
  556. free_vfsmnt(mnt);
  557. deactivate_super(sb);
  558. }
  559. void mntput_no_expire(struct vfsmount *mnt)
  560. {
  561. repeat:
  562. if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
  563. if (likely(!mnt->mnt_pinned)) {
  564. spin_unlock(&vfsmount_lock);
  565. __mntput(mnt);
  566. return;
  567. }
  568. atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
  569. mnt->mnt_pinned = 0;
  570. spin_unlock(&vfsmount_lock);
  571. acct_auto_close_mnt(mnt);
  572. goto repeat;
  573. }
  574. }
  575. EXPORT_SYMBOL(mntput_no_expire);
  576. void mnt_pin(struct vfsmount *mnt)
  577. {
  578. spin_lock(&vfsmount_lock);
  579. mnt->mnt_pinned++;
  580. spin_unlock(&vfsmount_lock);
  581. }
  582. EXPORT_SYMBOL(mnt_pin);
  583. void mnt_unpin(struct vfsmount *mnt)
  584. {
  585. spin_lock(&vfsmount_lock);
  586. if (mnt->mnt_pinned) {
  587. atomic_inc(&mnt->mnt_count);
  588. mnt->mnt_pinned--;
  589. }
  590. spin_unlock(&vfsmount_lock);
  591. }
  592. EXPORT_SYMBOL(mnt_unpin);
  593. static inline void mangle(struct seq_file *m, const char *s)
  594. {
  595. seq_escape(m, s, " \t\n\\");
  596. }
  597. /*
  598. * Simple .show_options callback for filesystems which don't want to
  599. * implement more complex mount option showing.
  600. *
  601. * See also save_mount_options().
  602. */
  603. int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
  604. {
  605. const char *options;
  606. rcu_read_lock();
  607. options = rcu_dereference(mnt->mnt_sb->s_options);
  608. if (options != NULL && options[0]) {
  609. seq_putc(m, ',');
  610. mangle(m, options);
  611. }
  612. rcu_read_unlock();
  613. return 0;
  614. }
  615. EXPORT_SYMBOL(generic_show_options);
  616. /*
  617. * If filesystem uses generic_show_options(), this function should be
  618. * called from the fill_super() callback.
  619. *
  620. * The .remount_fs callback usually needs to be handled in a special
  621. * way, to make sure, that previous options are not overwritten if the
  622. * remount fails.
  623. *
  624. * Also note, that if the filesystem's .remount_fs function doesn't
  625. * reset all options to their default value, but changes only newly
  626. * given options, then the displayed options will not reflect reality
  627. * any more.
  628. */
  629. void save_mount_options(struct super_block *sb, char *options)
  630. {
  631. BUG_ON(sb->s_options);
  632. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  633. }
  634. EXPORT_SYMBOL(save_mount_options);
  635. void replace_mount_options(struct super_block *sb, char *options)
  636. {
  637. char *old = sb->s_options;
  638. rcu_assign_pointer(sb->s_options, options);
  639. if (old) {
  640. synchronize_rcu();
  641. kfree(old);
  642. }
  643. }
  644. EXPORT_SYMBOL(replace_mount_options);
  645. #ifdef CONFIG_PROC_FS
  646. /* iterator */
  647. static void *m_start(struct seq_file *m, loff_t *pos)
  648. {
  649. struct proc_mounts *p = m->private;
  650. down_read(&namespace_sem);
  651. return seq_list_start(&p->ns->list, *pos);
  652. }
  653. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  654. {
  655. struct proc_mounts *p = m->private;
  656. return seq_list_next(v, &p->ns->list, pos);
  657. }
  658. static void m_stop(struct seq_file *m, void *v)
  659. {
  660. up_read(&namespace_sem);
  661. }
  662. int mnt_had_events(struct proc_mounts *p)
  663. {
  664. struct mnt_namespace *ns = p->ns;
  665. int res = 0;
  666. spin_lock(&vfsmount_lock);
  667. if (p->event != ns->event) {
  668. p->event = ns->event;
  669. res = 1;
  670. }
  671. spin_unlock(&vfsmount_lock);
  672. return res;
  673. }
  674. struct proc_fs_info {
  675. int flag;
  676. const char *str;
  677. };
  678. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  679. {
  680. static const struct proc_fs_info fs_info[] = {
  681. { MS_SYNCHRONOUS, ",sync" },
  682. { MS_DIRSYNC, ",dirsync" },
  683. { MS_MANDLOCK, ",mand" },
  684. { 0, NULL }
  685. };
  686. const struct proc_fs_info *fs_infop;
  687. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  688. if (sb->s_flags & fs_infop->flag)
  689. seq_puts(m, fs_infop->str);
  690. }
  691. return security_sb_show_options(m, sb);
  692. }
  693. static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
  694. {
  695. static const struct proc_fs_info mnt_info[] = {
  696. { MNT_NOSUID, ",nosuid" },
  697. { MNT_NODEV, ",nodev" },
  698. { MNT_NOEXEC, ",noexec" },
  699. { MNT_NOATIME, ",noatime" },
  700. { MNT_NODIRATIME, ",nodiratime" },
  701. { MNT_RELATIME, ",relatime" },
  702. { MNT_STRICTATIME, ",strictatime" },
  703. { 0, NULL }
  704. };
  705. const struct proc_fs_info *fs_infop;
  706. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  707. if (mnt->mnt_flags & fs_infop->flag)
  708. seq_puts(m, fs_infop->str);
  709. }
  710. }
  711. static void show_type(struct seq_file *m, struct super_block *sb)
  712. {
  713. mangle(m, sb->s_type->name);
  714. if (sb->s_subtype && sb->s_subtype[0]) {
  715. seq_putc(m, '.');
  716. mangle(m, sb->s_subtype);
  717. }
  718. }
  719. static int show_vfsmnt(struct seq_file *m, void *v)
  720. {
  721. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  722. int err = 0;
  723. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  724. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  725. seq_putc(m, ' ');
  726. seq_path(m, &mnt_path, " \t\n\\");
  727. seq_putc(m, ' ');
  728. show_type(m, mnt->mnt_sb);
  729. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  730. err = show_sb_opts(m, mnt->mnt_sb);
  731. if (err)
  732. goto out;
  733. show_mnt_opts(m, mnt);
  734. if (mnt->mnt_sb->s_op->show_options)
  735. err = mnt->mnt_sb->s_op->show_options(m, mnt);
  736. seq_puts(m, " 0 0\n");
  737. out:
  738. return err;
  739. }
  740. const struct seq_operations mounts_op = {
  741. .start = m_start,
  742. .next = m_next,
  743. .stop = m_stop,
  744. .show = show_vfsmnt
  745. };
  746. static int show_mountinfo(struct seq_file *m, void *v)
  747. {
  748. struct proc_mounts *p = m->private;
  749. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  750. struct super_block *sb = mnt->mnt_sb;
  751. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  752. struct path root = p->root;
  753. int err = 0;
  754. seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
  755. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  756. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  757. seq_putc(m, ' ');
  758. seq_path_root(m, &mnt_path, &root, " \t\n\\");
  759. if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
  760. /*
  761. * Mountpoint is outside root, discard that one. Ugly,
  762. * but less so than trying to do that in iterator in a
  763. * race-free way (due to renames).
  764. */
  765. return SEQ_SKIP;
  766. }
  767. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  768. show_mnt_opts(m, mnt);
  769. /* Tagged fields ("foo:X" or "bar") */
  770. if (IS_MNT_SHARED(mnt))
  771. seq_printf(m, " shared:%i", mnt->mnt_group_id);
  772. if (IS_MNT_SLAVE(mnt)) {
  773. int master = mnt->mnt_master->mnt_group_id;
  774. int dom = get_dominating_id(mnt, &p->root);
  775. seq_printf(m, " master:%i", master);
  776. if (dom && dom != master)
  777. seq_printf(m, " propagate_from:%i", dom);
  778. }
  779. if (IS_MNT_UNBINDABLE(mnt))
  780. seq_puts(m, " unbindable");
  781. /* Filesystem specific data */
  782. seq_puts(m, " - ");
  783. show_type(m, sb);
  784. seq_putc(m, ' ');
  785. mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
  786. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  787. err = show_sb_opts(m, sb);
  788. if (err)
  789. goto out;
  790. if (sb->s_op->show_options)
  791. err = sb->s_op->show_options(m, mnt);
  792. seq_putc(m, '\n');
  793. out:
  794. return err;
  795. }
  796. const struct seq_operations mountinfo_op = {
  797. .start = m_start,
  798. .next = m_next,
  799. .stop = m_stop,
  800. .show = show_mountinfo,
  801. };
  802. static int show_vfsstat(struct seq_file *m, void *v)
  803. {
  804. struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
  805. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  806. int err = 0;
  807. /* device */
  808. if (mnt->mnt_devname) {
  809. seq_puts(m, "device ");
  810. mangle(m, mnt->mnt_devname);
  811. } else
  812. seq_puts(m, "no device");
  813. /* mount point */
  814. seq_puts(m, " mounted on ");
  815. seq_path(m, &mnt_path, " \t\n\\");
  816. seq_putc(m, ' ');
  817. /* file system type */
  818. seq_puts(m, "with fstype ");
  819. show_type(m, mnt->mnt_sb);
  820. /* optional statistics */
  821. if (mnt->mnt_sb->s_op->show_stats) {
  822. seq_putc(m, ' ');
  823. err = mnt->mnt_sb->s_op->show_stats(m, mnt);
  824. }
  825. seq_putc(m, '\n');
  826. return err;
  827. }
  828. const struct seq_operations mountstats_op = {
  829. .start = m_start,
  830. .next = m_next,
  831. .stop = m_stop,
  832. .show = show_vfsstat,
  833. };
  834. #endif /* CONFIG_PROC_FS */
  835. /**
  836. * may_umount_tree - check if a mount tree is busy
  837. * @mnt: root of mount tree
  838. *
  839. * This is called to check if a tree of mounts has any
  840. * open files, pwds, chroots or sub mounts that are
  841. * busy.
  842. */
  843. int may_umount_tree(struct vfsmount *mnt)
  844. {
  845. int actual_refs = 0;
  846. int minimum_refs = 0;
  847. struct vfsmount *p;
  848. spin_lock(&vfsmount_lock);
  849. for (p = mnt; p; p = next_mnt(p, mnt)) {
  850. actual_refs += atomic_read(&p->mnt_count);
  851. minimum_refs += 2;
  852. }
  853. spin_unlock(&vfsmount_lock);
  854. if (actual_refs > minimum_refs)
  855. return 0;
  856. return 1;
  857. }
  858. EXPORT_SYMBOL(may_umount_tree);
  859. /**
  860. * may_umount - check if a mount point is busy
  861. * @mnt: root of mount
  862. *
  863. * This is called to check if a mount point has any
  864. * open files, pwds, chroots or sub mounts. If the
  865. * mount has sub mounts this will return busy
  866. * regardless of whether the sub mounts are busy.
  867. *
  868. * Doesn't take quota and stuff into account. IOW, in some cases it will
  869. * give false negatives. The main reason why it's here is that we need
  870. * a non-destructive way to look for easily umountable filesystems.
  871. */
  872. int may_umount(struct vfsmount *mnt)
  873. {
  874. int ret = 1;
  875. down_read(&namespace_sem);
  876. spin_lock(&vfsmount_lock);
  877. if (propagate_mount_busy(mnt, 2))
  878. ret = 0;
  879. spin_unlock(&vfsmount_lock);
  880. up_read(&namespace_sem);
  881. return ret;
  882. }
  883. EXPORT_SYMBOL(may_umount);
  884. void release_mounts(struct list_head *head)
  885. {
  886. struct vfsmount *mnt;
  887. while (!list_empty(head)) {
  888. mnt = list_first_entry(head, struct vfsmount, mnt_hash);
  889. list_del_init(&mnt->mnt_hash);
  890. if (mnt->mnt_parent != mnt) {
  891. struct dentry *dentry;
  892. struct vfsmount *m;
  893. spin_lock(&vfsmount_lock);
  894. dentry = mnt->mnt_mountpoint;
  895. m = mnt->mnt_parent;
  896. mnt->mnt_mountpoint = mnt->mnt_root;
  897. mnt->mnt_parent = mnt;
  898. m->mnt_ghosts--;
  899. spin_unlock(&vfsmount_lock);
  900. dput(dentry);
  901. mntput(m);
  902. }
  903. mntput(mnt);
  904. }
  905. }
  906. void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
  907. {
  908. struct vfsmount *p;
  909. for (p = mnt; p; p = next_mnt(p, mnt))
  910. list_move(&p->mnt_hash, kill);
  911. if (propagate)
  912. propagate_umount(kill);
  913. list_for_each_entry(p, kill, mnt_hash) {
  914. list_del_init(&p->mnt_expire);
  915. list_del_init(&p->mnt_list);
  916. __touch_mnt_namespace(p->mnt_ns);
  917. p->mnt_ns = NULL;
  918. list_del_init(&p->mnt_child);
  919. if (p->mnt_parent != p) {
  920. p->mnt_parent->mnt_ghosts++;
  921. p->mnt_mountpoint->d_mounted--;
  922. }
  923. change_mnt_propagation(p, MS_PRIVATE);
  924. }
  925. }
  926. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
  927. static int do_umount(struct vfsmount *mnt, int flags)
  928. {
  929. struct super_block *sb = mnt->mnt_sb;
  930. int retval;
  931. LIST_HEAD(umount_list);
  932. retval = security_sb_umount(mnt, flags);
  933. if (retval)
  934. return retval;
  935. /*
  936. * Allow userspace to request a mountpoint be expired rather than
  937. * unmounting unconditionally. Unmount only happens if:
  938. * (1) the mark is already set (the mark is cleared by mntput())
  939. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  940. */
  941. if (flags & MNT_EXPIRE) {
  942. if (mnt == current->fs->root.mnt ||
  943. flags & (MNT_FORCE | MNT_DETACH))
  944. return -EINVAL;
  945. if (atomic_read(&mnt->mnt_count) != 2)
  946. return -EBUSY;
  947. if (!xchg(&mnt->mnt_expiry_mark, 1))
  948. return -EAGAIN;
  949. }
  950. /*
  951. * If we may have to abort operations to get out of this
  952. * mount, and they will themselves hold resources we must
  953. * allow the fs to do things. In the Unix tradition of
  954. * 'Gee thats tricky lets do it in userspace' the umount_begin
  955. * might fail to complete on the first run through as other tasks
  956. * must return, and the like. Thats for the mount program to worry
  957. * about for the moment.
  958. */
  959. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  960. sb->s_op->umount_begin(sb);
  961. }
  962. /*
  963. * No sense to grab the lock for this test, but test itself looks
  964. * somewhat bogus. Suggestions for better replacement?
  965. * Ho-hum... In principle, we might treat that as umount + switch
  966. * to rootfs. GC would eventually take care of the old vfsmount.
  967. * Actually it makes sense, especially if rootfs would contain a
  968. * /reboot - static binary that would close all descriptors and
  969. * call reboot(9). Then init(8) could umount root and exec /reboot.
  970. */
  971. if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  972. /*
  973. * Special case for "unmounting" root ...
  974. * we just try to remount it readonly.
  975. */
  976. down_write(&sb->s_umount);
  977. if (!(sb->s_flags & MS_RDONLY))
  978. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  979. up_write(&sb->s_umount);
  980. return retval;
  981. }
  982. down_write(&namespace_sem);
  983. spin_lock(&vfsmount_lock);
  984. event++;
  985. if (!(flags & MNT_DETACH))
  986. shrink_submounts(mnt, &umount_list);
  987. retval = -EBUSY;
  988. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  989. if (!list_empty(&mnt->mnt_list))
  990. umount_tree(mnt, 1, &umount_list);
  991. retval = 0;
  992. }
  993. spin_unlock(&vfsmount_lock);
  994. up_write(&namespace_sem);
  995. release_mounts(&umount_list);
  996. return retval;
  997. }
  998. /*
  999. * Now umount can handle mount points as well as block devices.
  1000. * This is important for filesystems which use unnamed block devices.
  1001. *
  1002. * We now support a flag for forced unmount like the other 'big iron'
  1003. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1004. */
  1005. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1006. {
  1007. struct path path;
  1008. int retval;
  1009. int lookup_flags = 0;
  1010. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1011. return -EINVAL;
  1012. if (!(flags & UMOUNT_NOFOLLOW))
  1013. lookup_flags |= LOOKUP_FOLLOW;
  1014. retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
  1015. if (retval)
  1016. goto out;
  1017. retval = -EINVAL;
  1018. if (path.dentry != path.mnt->mnt_root)
  1019. goto dput_and_out;
  1020. if (!check_mnt(path.mnt))
  1021. goto dput_and_out;
  1022. retval = -EPERM;
  1023. if (!capable(CAP_SYS_ADMIN))
  1024. goto dput_and_out;
  1025. retval = do_umount(path.mnt, flags);
  1026. dput_and_out:
  1027. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1028. dput(path.dentry);
  1029. mntput_no_expire(path.mnt);
  1030. out:
  1031. return retval;
  1032. }
  1033. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1034. /*
  1035. * The 2.0 compatible umount. No flags.
  1036. */
  1037. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1038. {
  1039. return sys_umount(name, 0);
  1040. }
  1041. #endif
  1042. static int mount_is_safe(struct path *path)
  1043. {
  1044. if (capable(CAP_SYS_ADMIN))
  1045. return 0;
  1046. return -EPERM;
  1047. #ifdef notyet
  1048. if (S_ISLNK(path->dentry->d_inode->i_mode))
  1049. return -EPERM;
  1050. if (path->dentry->d_inode->i_mode & S_ISVTX) {
  1051. if (current_uid() != path->dentry->d_inode->i_uid)
  1052. return -EPERM;
  1053. }
  1054. if (inode_permission(path->dentry->d_inode, MAY_WRITE))
  1055. return -EPERM;
  1056. return 0;
  1057. #endif
  1058. }
  1059. struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
  1060. int flag)
  1061. {
  1062. struct vfsmount *res, *p, *q, *r, *s;
  1063. struct path path;
  1064. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  1065. return NULL;
  1066. res = q = clone_mnt(mnt, dentry, flag);
  1067. if (!q)
  1068. goto Enomem;
  1069. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1070. p = mnt;
  1071. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1072. if (!is_subdir(r->mnt_mountpoint, dentry))
  1073. continue;
  1074. for (s = r; s; s = next_mnt(s, r)) {
  1075. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  1076. s = skip_mnt_tree(s);
  1077. continue;
  1078. }
  1079. while (p != s->mnt_parent) {
  1080. p = p->mnt_parent;
  1081. q = q->mnt_parent;
  1082. }
  1083. p = s;
  1084. path.mnt = q;
  1085. path.dentry = p->mnt_mountpoint;
  1086. q = clone_mnt(p, p->mnt_root, flag);
  1087. if (!q)
  1088. goto Enomem;
  1089. spin_lock(&vfsmount_lock);
  1090. list_add_tail(&q->mnt_list, &res->mnt_list);
  1091. attach_mnt(q, &path);
  1092. spin_unlock(&vfsmount_lock);
  1093. }
  1094. }
  1095. return res;
  1096. Enomem:
  1097. if (res) {
  1098. LIST_HEAD(umount_list);
  1099. spin_lock(&vfsmount_lock);
  1100. umount_tree(res, 0, &umount_list);
  1101. spin_unlock(&vfsmount_lock);
  1102. release_mounts(&umount_list);
  1103. }
  1104. return NULL;
  1105. }
  1106. struct vfsmount *collect_mounts(struct path *path)
  1107. {
  1108. struct vfsmount *tree;
  1109. down_write(&namespace_sem);
  1110. tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
  1111. up_write(&namespace_sem);
  1112. return tree;
  1113. }
  1114. void drop_collected_mounts(struct vfsmount *mnt)
  1115. {
  1116. LIST_HEAD(umount_list);
  1117. down_write(&namespace_sem);
  1118. spin_lock(&vfsmount_lock);
  1119. umount_tree(mnt, 0, &umount_list);
  1120. spin_unlock(&vfsmount_lock);
  1121. up_write(&namespace_sem);
  1122. release_mounts(&umount_list);
  1123. }
  1124. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1125. struct vfsmount *root)
  1126. {
  1127. struct vfsmount *mnt;
  1128. int res = f(root, arg);
  1129. if (res)
  1130. return res;
  1131. list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
  1132. res = f(mnt, arg);
  1133. if (res)
  1134. return res;
  1135. }
  1136. return 0;
  1137. }
  1138. static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
  1139. {
  1140. struct vfsmount *p;
  1141. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1142. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1143. mnt_release_group_id(p);
  1144. }
  1145. }
  1146. static int invent_group_ids(struct vfsmount *mnt, bool recurse)
  1147. {
  1148. struct vfsmount *p;
  1149. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1150. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1151. int err = mnt_alloc_group_id(p);
  1152. if (err) {
  1153. cleanup_group_ids(mnt, p);
  1154. return err;
  1155. }
  1156. }
  1157. }
  1158. return 0;
  1159. }
  1160. /*
  1161. * @source_mnt : mount tree to be attached
  1162. * @nd : place the mount tree @source_mnt is attached
  1163. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1164. * store the parent mount and mountpoint dentry.
  1165. * (done when source_mnt is moved)
  1166. *
  1167. * NOTE: in the table below explains the semantics when a source mount
  1168. * of a given type is attached to a destination mount of a given type.
  1169. * ---------------------------------------------------------------------------
  1170. * | BIND MOUNT OPERATION |
  1171. * |**************************************************************************
  1172. * | source-->| shared | private | slave | unbindable |
  1173. * | dest | | | | |
  1174. * | | | | | | |
  1175. * | v | | | | |
  1176. * |**************************************************************************
  1177. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1178. * | | | | | |
  1179. * |non-shared| shared (+) | private | slave (*) | invalid |
  1180. * ***************************************************************************
  1181. * A bind operation clones the source mount and mounts the clone on the
  1182. * destination mount.
  1183. *
  1184. * (++) the cloned mount is propagated to all the mounts in the propagation
  1185. * tree of the destination mount and the cloned mount is added to
  1186. * the peer group of the source mount.
  1187. * (+) the cloned mount is created under the destination mount and is marked
  1188. * as shared. The cloned mount is added to the peer group of the source
  1189. * mount.
  1190. * (+++) the mount is propagated to all the mounts in the propagation tree
  1191. * of the destination mount and the cloned mount is made slave
  1192. * of the same master as that of the source mount. The cloned mount
  1193. * is marked as 'shared and slave'.
  1194. * (*) the cloned mount is made a slave of the same master as that of the
  1195. * source mount.
  1196. *
  1197. * ---------------------------------------------------------------------------
  1198. * | MOVE MOUNT OPERATION |
  1199. * |**************************************************************************
  1200. * | source-->| shared | private | slave | unbindable |
  1201. * | dest | | | | |
  1202. * | | | | | | |
  1203. * | v | | | | |
  1204. * |**************************************************************************
  1205. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1206. * | | | | | |
  1207. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1208. * ***************************************************************************
  1209. *
  1210. * (+) the mount is moved to the destination. And is then propagated to
  1211. * all the mounts in the propagation tree of the destination mount.
  1212. * (+*) the mount is moved to the destination.
  1213. * (+++) the mount is moved to the destination and is then propagated to
  1214. * all the mounts belonging to the destination mount's propagation tree.
  1215. * the mount is marked as 'shared and slave'.
  1216. * (*) the mount continues to be a slave at the new location.
  1217. *
  1218. * if the source mount is a tree, the operations explained above is
  1219. * applied to each mount in the tree.
  1220. * Must be called without spinlocks held, since this function can sleep
  1221. * in allocations.
  1222. */
  1223. static int attach_recursive_mnt(struct vfsmount *source_mnt,
  1224. struct path *path, struct path *parent_path)
  1225. {
  1226. LIST_HEAD(tree_list);
  1227. struct vfsmount *dest_mnt = path->mnt;
  1228. struct dentry *dest_dentry = path->dentry;
  1229. struct vfsmount *child, *p;
  1230. int err;
  1231. if (IS_MNT_SHARED(dest_mnt)) {
  1232. err = invent_group_ids(source_mnt, true);
  1233. if (err)
  1234. goto out;
  1235. }
  1236. err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
  1237. if (err)
  1238. goto out_cleanup_ids;
  1239. spin_lock(&vfsmount_lock);
  1240. if (IS_MNT_SHARED(dest_mnt)) {
  1241. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1242. set_mnt_shared(p);
  1243. }
  1244. if (parent_path) {
  1245. detach_mnt(source_mnt, parent_path);
  1246. attach_mnt(source_mnt, path);
  1247. touch_mnt_namespace(parent_path->mnt->mnt_ns);
  1248. } else {
  1249. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  1250. commit_tree(source_mnt);
  1251. }
  1252. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  1253. list_del_init(&child->mnt_hash);
  1254. commit_tree(child);
  1255. }
  1256. spin_unlock(&vfsmount_lock);
  1257. return 0;
  1258. out_cleanup_ids:
  1259. if (IS_MNT_SHARED(dest_mnt))
  1260. cleanup_group_ids(source_mnt, NULL);
  1261. out:
  1262. return err;
  1263. }
  1264. static int graft_tree(struct vfsmount *mnt, struct path *path)
  1265. {
  1266. int err;
  1267. if (mnt->mnt_sb->s_flags & MS_NOUSER)
  1268. return -EINVAL;
  1269. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1270. S_ISDIR(mnt->mnt_root->d_inode->i_mode))
  1271. return -ENOTDIR;
  1272. err = -ENOENT;
  1273. mutex_lock(&path->dentry->d_inode->i_mutex);
  1274. if (cant_mount(path->dentry))
  1275. goto out_unlock;
  1276. if (!d_unlinked(path->dentry))
  1277. err = attach_recursive_mnt(mnt, path, NULL);
  1278. out_unlock:
  1279. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1280. return err;
  1281. }
  1282. /*
  1283. * recursively change the type of the mountpoint.
  1284. */
  1285. static int do_change_type(struct path *path, int flag)
  1286. {
  1287. struct vfsmount *m, *mnt = path->mnt;
  1288. int recurse = flag & MS_REC;
  1289. int type = flag & ~MS_REC;
  1290. int err = 0;
  1291. if (!capable(CAP_SYS_ADMIN))
  1292. return -EPERM;
  1293. if (path->dentry != path->mnt->mnt_root)
  1294. return -EINVAL;
  1295. down_write(&namespace_sem);
  1296. if (type == MS_SHARED) {
  1297. err = invent_group_ids(mnt, recurse);
  1298. if (err)
  1299. goto out_unlock;
  1300. }
  1301. spin_lock(&vfsmount_lock);
  1302. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1303. change_mnt_propagation(m, type);
  1304. spin_unlock(&vfsmount_lock);
  1305. out_unlock:
  1306. up_write(&namespace_sem);
  1307. return err;
  1308. }
  1309. /*
  1310. * do loopback mount.
  1311. */
  1312. static int do_loopback(struct path *path, char *old_name,
  1313. int recurse)
  1314. {
  1315. struct path old_path;
  1316. struct vfsmount *mnt = NULL;
  1317. int err = mount_is_safe(path);
  1318. if (err)
  1319. return err;
  1320. if (!old_name || !*old_name)
  1321. return -EINVAL;
  1322. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1323. if (err)
  1324. return err;
  1325. down_write(&namespace_sem);
  1326. err = -EINVAL;
  1327. if (IS_MNT_UNBINDABLE(old_path.mnt))
  1328. goto out;
  1329. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1330. goto out;
  1331. err = -ENOMEM;
  1332. if (recurse)
  1333. mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
  1334. else
  1335. mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
  1336. if (!mnt)
  1337. goto out;
  1338. err = graft_tree(mnt, path);
  1339. if (err) {
  1340. LIST_HEAD(umount_list);
  1341. spin_lock(&vfsmount_lock);
  1342. umount_tree(mnt, 0, &umount_list);
  1343. spin_unlock(&vfsmount_lock);
  1344. release_mounts(&umount_list);
  1345. }
  1346. out:
  1347. up_write(&namespace_sem);
  1348. path_put(&old_path);
  1349. return err;
  1350. }
  1351. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1352. {
  1353. int error = 0;
  1354. int readonly_request = 0;
  1355. if (ms_flags & MS_RDONLY)
  1356. readonly_request = 1;
  1357. if (readonly_request == __mnt_is_readonly(mnt))
  1358. return 0;
  1359. if (readonly_request)
  1360. error = mnt_make_readonly(mnt);
  1361. else
  1362. __mnt_unmake_readonly(mnt);
  1363. return error;
  1364. }
  1365. /*
  1366. * change filesystem flags. dir should be a physical root of filesystem.
  1367. * If you've mounted a non-root directory somewhere and want to do remount
  1368. * on it - tough luck.
  1369. */
  1370. static int do_remount(struct path *path, int flags, int mnt_flags,
  1371. void *data)
  1372. {
  1373. int err;
  1374. struct super_block *sb = path->mnt->mnt_sb;
  1375. if (!capable(CAP_SYS_ADMIN))
  1376. return -EPERM;
  1377. if (!check_mnt(path->mnt))
  1378. return -EINVAL;
  1379. if (path->dentry != path->mnt->mnt_root)
  1380. return -EINVAL;
  1381. down_write(&sb->s_umount);
  1382. if (flags & MS_BIND)
  1383. err = change_mount_flags(path->mnt, flags);
  1384. else
  1385. err = do_remount_sb(sb, flags, data, 0);
  1386. if (!err) {
  1387. spin_lock(&vfsmount_lock);
  1388. mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
  1389. path->mnt->mnt_flags = mnt_flags;
  1390. spin_unlock(&vfsmount_lock);
  1391. }
  1392. up_write(&sb->s_umount);
  1393. if (!err) {
  1394. spin_lock(&vfsmount_lock);
  1395. touch_mnt_namespace(path->mnt->mnt_ns);
  1396. spin_unlock(&vfsmount_lock);
  1397. }
  1398. return err;
  1399. }
  1400. static inline int tree_contains_unbindable(struct vfsmount *mnt)
  1401. {
  1402. struct vfsmount *p;
  1403. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1404. if (IS_MNT_UNBINDABLE(p))
  1405. return 1;
  1406. }
  1407. return 0;
  1408. }
  1409. static int do_move_mount(struct path *path, char *old_name)
  1410. {
  1411. struct path old_path, parent_path;
  1412. struct vfsmount *p;
  1413. int err = 0;
  1414. if (!capable(CAP_SYS_ADMIN))
  1415. return -EPERM;
  1416. if (!old_name || !*old_name)
  1417. return -EINVAL;
  1418. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1419. if (err)
  1420. return err;
  1421. down_write(&namespace_sem);
  1422. while (d_mountpoint(path->dentry) &&
  1423. follow_down(path))
  1424. ;
  1425. err = -EINVAL;
  1426. if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
  1427. goto out;
  1428. err = -ENOENT;
  1429. mutex_lock(&path->dentry->d_inode->i_mutex);
  1430. if (cant_mount(path->dentry))
  1431. goto out1;
  1432. if (d_unlinked(path->dentry))
  1433. goto out1;
  1434. err = -EINVAL;
  1435. if (old_path.dentry != old_path.mnt->mnt_root)
  1436. goto out1;
  1437. if (old_path.mnt == old_path.mnt->mnt_parent)
  1438. goto out1;
  1439. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1440. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1441. goto out1;
  1442. /*
  1443. * Don't move a mount residing in a shared parent.
  1444. */
  1445. if (old_path.mnt->mnt_parent &&
  1446. IS_MNT_SHARED(old_path.mnt->mnt_parent))
  1447. goto out1;
  1448. /*
  1449. * Don't move a mount tree containing unbindable mounts to a destination
  1450. * mount which is shared.
  1451. */
  1452. if (IS_MNT_SHARED(path->mnt) &&
  1453. tree_contains_unbindable(old_path.mnt))
  1454. goto out1;
  1455. err = -ELOOP;
  1456. for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
  1457. if (p == old_path.mnt)
  1458. goto out1;
  1459. err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
  1460. if (err)
  1461. goto out1;
  1462. /* if the mount is moved, it should no longer be expire
  1463. * automatically */
  1464. list_del_init(&old_path.mnt->mnt_expire);
  1465. out1:
  1466. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1467. out:
  1468. up_write(&namespace_sem);
  1469. if (!err)
  1470. path_put(&parent_path);
  1471. path_put(&old_path);
  1472. return err;
  1473. }
  1474. /*
  1475. * create a new mount for userspace and request it to be added into the
  1476. * namespace's tree
  1477. */
  1478. static int do_new_mount(struct path *path, char *type, int flags,
  1479. int mnt_flags, char *name, void *data)
  1480. {
  1481. struct vfsmount *mnt;
  1482. if (!type)
  1483. return -EINVAL;
  1484. /* we need capabilities... */
  1485. if (!capable(CAP_SYS_ADMIN))
  1486. return -EPERM;
  1487. lock_kernel();
  1488. mnt = do_kern_mount(type, flags, name, data);
  1489. unlock_kernel();
  1490. if (IS_ERR(mnt))
  1491. return PTR_ERR(mnt);
  1492. return do_add_mount(mnt, path, mnt_flags, NULL);
  1493. }
  1494. /*
  1495. * add a mount into a namespace's mount tree
  1496. * - provide the option of adding the new mount to an expiration list
  1497. */
  1498. int do_add_mount(struct vfsmount *newmnt, struct path *path,
  1499. int mnt_flags, struct list_head *fslist)
  1500. {
  1501. int err;
  1502. mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
  1503. down_write(&namespace_sem);
  1504. /* Something was mounted here while we slept */
  1505. while (d_mountpoint(path->dentry) &&
  1506. follow_down(path))
  1507. ;
  1508. err = -EINVAL;
  1509. if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
  1510. goto unlock;
  1511. /* Refuse the same filesystem on the same mount point */
  1512. err = -EBUSY;
  1513. if (path->mnt->mnt_sb == newmnt->mnt_sb &&
  1514. path->mnt->mnt_root == path->dentry)
  1515. goto unlock;
  1516. err = -EINVAL;
  1517. if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
  1518. goto unlock;
  1519. newmnt->mnt_flags = mnt_flags;
  1520. if ((err = graft_tree(newmnt, path)))
  1521. goto unlock;
  1522. if (fslist) /* add to the specified expiration list */
  1523. list_add_tail(&newmnt->mnt_expire, fslist);
  1524. up_write(&namespace_sem);
  1525. return 0;
  1526. unlock:
  1527. up_write(&namespace_sem);
  1528. mntput(newmnt);
  1529. return err;
  1530. }
  1531. EXPORT_SYMBOL_GPL(do_add_mount);
  1532. /*
  1533. * process a list of expirable mountpoints with the intent of discarding any
  1534. * mountpoints that aren't in use and haven't been touched since last we came
  1535. * here
  1536. */
  1537. void mark_mounts_for_expiry(struct list_head *mounts)
  1538. {
  1539. struct vfsmount *mnt, *next;
  1540. LIST_HEAD(graveyard);
  1541. LIST_HEAD(umounts);
  1542. if (list_empty(mounts))
  1543. return;
  1544. down_write(&namespace_sem);
  1545. spin_lock(&vfsmount_lock);
  1546. /* extract from the expiration list every vfsmount that matches the
  1547. * following criteria:
  1548. * - only referenced by its parent vfsmount
  1549. * - still marked for expiry (marked on the last call here; marks are
  1550. * cleared by mntput())
  1551. */
  1552. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1553. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1554. propagate_mount_busy(mnt, 1))
  1555. continue;
  1556. list_move(&mnt->mnt_expire, &graveyard);
  1557. }
  1558. while (!list_empty(&graveyard)) {
  1559. mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
  1560. touch_mnt_namespace(mnt->mnt_ns);
  1561. umount_tree(mnt, 1, &umounts);
  1562. }
  1563. spin_unlock(&vfsmount_lock);
  1564. up_write(&namespace_sem);
  1565. release_mounts(&umounts);
  1566. }
  1567. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1568. /*
  1569. * Ripoff of 'select_parent()'
  1570. *
  1571. * search the list of submounts for a given mountpoint, and move any
  1572. * shrinkable submounts to the 'graveyard' list.
  1573. */
  1574. static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
  1575. {
  1576. struct vfsmount *this_parent = parent;
  1577. struct list_head *next;
  1578. int found = 0;
  1579. repeat:
  1580. next = this_parent->mnt_mounts.next;
  1581. resume:
  1582. while (next != &this_parent->mnt_mounts) {
  1583. struct list_head *tmp = next;
  1584. struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
  1585. next = tmp->next;
  1586. if (!(mnt->mnt_flags & MNT_SHRINKABLE))
  1587. continue;
  1588. /*
  1589. * Descend a level if the d_mounts list is non-empty.
  1590. */
  1591. if (!list_empty(&mnt->mnt_mounts)) {
  1592. this_parent = mnt;
  1593. goto repeat;
  1594. }
  1595. if (!propagate_mount_busy(mnt, 1)) {
  1596. list_move_tail(&mnt->mnt_expire, graveyard);
  1597. found++;
  1598. }
  1599. }
  1600. /*
  1601. * All done at this level ... ascend and resume the search
  1602. */
  1603. if (this_parent != parent) {
  1604. next = this_parent->mnt_child.next;
  1605. this_parent = this_parent->mnt_parent;
  1606. goto resume;
  1607. }
  1608. return found;
  1609. }
  1610. /*
  1611. * process a list of expirable mountpoints with the intent of discarding any
  1612. * submounts of a specific parent mountpoint
  1613. */
  1614. static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
  1615. {
  1616. LIST_HEAD(graveyard);
  1617. struct vfsmount *m;
  1618. /* extract submounts of 'mountpoint' from the expiration list */
  1619. while (select_submounts(mnt, &graveyard)) {
  1620. while (!list_empty(&graveyard)) {
  1621. m = list_first_entry(&graveyard, struct vfsmount,
  1622. mnt_expire);
  1623. touch_mnt_namespace(m->mnt_ns);
  1624. umount_tree(m, 1, umounts);
  1625. }
  1626. }
  1627. }
  1628. /*
  1629. * Some copy_from_user() implementations do not return the exact number of
  1630. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1631. * Note that this function differs from copy_from_user() in that it will oops
  1632. * on bad values of `to', rather than returning a short copy.
  1633. */
  1634. static long exact_copy_from_user(void *to, const void __user * from,
  1635. unsigned long n)
  1636. {
  1637. char *t = to;
  1638. const char __user *f = from;
  1639. char c;
  1640. if (!access_ok(VERIFY_READ, from, n))
  1641. return n;
  1642. while (n) {
  1643. if (__get_user(c, f)) {
  1644. memset(t, 0, n);
  1645. break;
  1646. }
  1647. *t++ = c;
  1648. f++;
  1649. n--;
  1650. }
  1651. return n;
  1652. }
  1653. int copy_mount_options(const void __user * data, unsigned long *where)
  1654. {
  1655. int i;
  1656. unsigned long page;
  1657. unsigned long size;
  1658. *where = 0;
  1659. if (!data)
  1660. return 0;
  1661. if (!(page = __get_free_page(GFP_KERNEL)))
  1662. return -ENOMEM;
  1663. /* We only care that *some* data at the address the user
  1664. * gave us is valid. Just in case, we'll zero
  1665. * the remainder of the page.
  1666. */
  1667. /* copy_from_user cannot cross TASK_SIZE ! */
  1668. size = TASK_SIZE - (unsigned long)data;
  1669. if (size > PAGE_SIZE)
  1670. size = PAGE_SIZE;
  1671. i = size - exact_copy_from_user((void *)page, data, size);
  1672. if (!i) {
  1673. free_page(page);
  1674. return -EFAULT;
  1675. }
  1676. if (i != PAGE_SIZE)
  1677. memset((char *)page + i, 0, PAGE_SIZE - i);
  1678. *where = page;
  1679. return 0;
  1680. }
  1681. int copy_mount_string(const void __user *data, char **where)
  1682. {
  1683. char *tmp;
  1684. if (!data) {
  1685. *where = NULL;
  1686. return 0;
  1687. }
  1688. tmp = strndup_user(data, PAGE_SIZE);
  1689. if (IS_ERR(tmp))
  1690. return PTR_ERR(tmp);
  1691. *where = tmp;
  1692. return 0;
  1693. }
  1694. /*
  1695. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1696. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1697. *
  1698. * data is a (void *) that can point to any structure up to
  1699. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1700. * information (or be NULL).
  1701. *
  1702. * Pre-0.97 versions of mount() didn't have a flags word.
  1703. * When the flags word was introduced its top half was required
  1704. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1705. * Therefore, if this magic number is present, it carries no information
  1706. * and must be discarded.
  1707. */
  1708. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1709. unsigned long flags, void *data_page)
  1710. {
  1711. struct path path;
  1712. int retval = 0;
  1713. int mnt_flags = 0;
  1714. /* Discard magic */
  1715. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  1716. flags &= ~MS_MGC_MSK;
  1717. /* Basic sanity checks */
  1718. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  1719. return -EINVAL;
  1720. if (data_page)
  1721. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  1722. /* ... and get the mountpoint */
  1723. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  1724. if (retval)
  1725. return retval;
  1726. retval = security_sb_mount(dev_name, &path,
  1727. type_page, flags, data_page);
  1728. if (retval)
  1729. goto dput_out;
  1730. /* Default to relatime unless overriden */
  1731. if (!(flags & MS_NOATIME))
  1732. mnt_flags |= MNT_RELATIME;
  1733. /* Separate the per-mountpoint flags */
  1734. if (flags & MS_NOSUID)
  1735. mnt_flags |= MNT_NOSUID;
  1736. if (flags & MS_NODEV)
  1737. mnt_flags |= MNT_NODEV;
  1738. if (flags & MS_NOEXEC)
  1739. mnt_flags |= MNT_NOEXEC;
  1740. if (flags & MS_NOATIME)
  1741. mnt_flags |= MNT_NOATIME;
  1742. if (flags & MS_NODIRATIME)
  1743. mnt_flags |= MNT_NODIRATIME;
  1744. if (flags & MS_STRICTATIME)
  1745. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  1746. if (flags & MS_RDONLY)
  1747. mnt_flags |= MNT_READONLY;
  1748. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  1749. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  1750. MS_STRICTATIME);
  1751. if (flags & MS_REMOUNT)
  1752. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  1753. data_page);
  1754. else if (flags & MS_BIND)
  1755. retval = do_loopback(&path, dev_name, flags & MS_REC);
  1756. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1757. retval = do_change_type(&path, flags);
  1758. else if (flags & MS_MOVE)
  1759. retval = do_move_mount(&path, dev_name);
  1760. else
  1761. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  1762. dev_name, data_page);
  1763. dput_out:
  1764. path_put(&path);
  1765. return retval;
  1766. }
  1767. static struct mnt_namespace *alloc_mnt_ns(void)
  1768. {
  1769. struct mnt_namespace *new_ns;
  1770. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  1771. if (!new_ns)
  1772. return ERR_PTR(-ENOMEM);
  1773. atomic_set(&new_ns->count, 1);
  1774. new_ns->root = NULL;
  1775. INIT_LIST_HEAD(&new_ns->list);
  1776. init_waitqueue_head(&new_ns->poll);
  1777. new_ns->event = 0;
  1778. return new_ns;
  1779. }
  1780. /*
  1781. * Allocate a new namespace structure and populate it with contents
  1782. * copied from the namespace of the passed in task structure.
  1783. */
  1784. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  1785. struct fs_struct *fs)
  1786. {
  1787. struct mnt_namespace *new_ns;
  1788. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  1789. struct vfsmount *p, *q;
  1790. new_ns = alloc_mnt_ns();
  1791. if (IS_ERR(new_ns))
  1792. return new_ns;
  1793. down_write(&namespace_sem);
  1794. /* First pass: copy the tree topology */
  1795. new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
  1796. CL_COPY_ALL | CL_EXPIRE);
  1797. if (!new_ns->root) {
  1798. up_write(&namespace_sem);
  1799. kfree(new_ns);
  1800. return ERR_PTR(-ENOMEM);
  1801. }
  1802. spin_lock(&vfsmount_lock);
  1803. list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
  1804. spin_unlock(&vfsmount_lock);
  1805. /*
  1806. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  1807. * as belonging to new namespace. We have already acquired a private
  1808. * fs_struct, so tsk->fs->lock is not needed.
  1809. */
  1810. p = mnt_ns->root;
  1811. q = new_ns->root;
  1812. while (p) {
  1813. q->mnt_ns = new_ns;
  1814. if (fs) {
  1815. if (p == fs->root.mnt) {
  1816. rootmnt = p;
  1817. fs->root.mnt = mntget(q);
  1818. }
  1819. if (p == fs->pwd.mnt) {
  1820. pwdmnt = p;
  1821. fs->pwd.mnt = mntget(q);
  1822. }
  1823. }
  1824. p = next_mnt(p, mnt_ns->root);
  1825. q = next_mnt(q, new_ns->root);
  1826. }
  1827. up_write(&namespace_sem);
  1828. if (rootmnt)
  1829. mntput(rootmnt);
  1830. if (pwdmnt)
  1831. mntput(pwdmnt);
  1832. return new_ns;
  1833. }
  1834. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  1835. struct fs_struct *new_fs)
  1836. {
  1837. struct mnt_namespace *new_ns;
  1838. BUG_ON(!ns);
  1839. get_mnt_ns(ns);
  1840. if (!(flags & CLONE_NEWNS))
  1841. return ns;
  1842. new_ns = dup_mnt_ns(ns, new_fs);
  1843. put_mnt_ns(ns);
  1844. return new_ns;
  1845. }
  1846. /**
  1847. * create_mnt_ns - creates a private namespace and adds a root filesystem
  1848. * @mnt: pointer to the new root filesystem mountpoint
  1849. */
  1850. struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
  1851. {
  1852. struct mnt_namespace *new_ns;
  1853. new_ns = alloc_mnt_ns();
  1854. if (!IS_ERR(new_ns)) {
  1855. mnt->mnt_ns = new_ns;
  1856. new_ns->root = mnt;
  1857. list_add(&new_ns->list, &new_ns->root->mnt_list);
  1858. }
  1859. return new_ns;
  1860. }
  1861. EXPORT_SYMBOL(create_mnt_ns);
  1862. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  1863. char __user *, type, unsigned long, flags, void __user *, data)
  1864. {
  1865. int ret;
  1866. char *kernel_type;
  1867. char *kernel_dir;
  1868. char *kernel_dev;
  1869. unsigned long data_page;
  1870. ret = copy_mount_string(type, &kernel_type);
  1871. if (ret < 0)
  1872. goto out_type;
  1873. kernel_dir = getname(dir_name);
  1874. if (IS_ERR(kernel_dir)) {
  1875. ret = PTR_ERR(kernel_dir);
  1876. goto out_dir;
  1877. }
  1878. ret = copy_mount_string(dev_name, &kernel_dev);
  1879. if (ret < 0)
  1880. goto out_dev;
  1881. ret = copy_mount_options(data, &data_page);
  1882. if (ret < 0)
  1883. goto out_data;
  1884. ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
  1885. (void *) data_page);
  1886. free_page(data_page);
  1887. out_data:
  1888. kfree(kernel_dev);
  1889. out_dev:
  1890. putname(kernel_dir);
  1891. out_dir:
  1892. kfree(kernel_type);
  1893. out_type:
  1894. return ret;
  1895. }
  1896. /*
  1897. * pivot_root Semantics:
  1898. * Moves the root file system of the current process to the directory put_old,
  1899. * makes new_root as the new root file system of the current process, and sets
  1900. * root/cwd of all processes which had them on the current root to new_root.
  1901. *
  1902. * Restrictions:
  1903. * The new_root and put_old must be directories, and must not be on the
  1904. * same file system as the current process root. The put_old must be
  1905. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  1906. * pointed to by put_old must yield the same directory as new_root. No other
  1907. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  1908. *
  1909. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  1910. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  1911. * in this situation.
  1912. *
  1913. * Notes:
  1914. * - we don't move root/cwd if they are not at the root (reason: if something
  1915. * cared enough to change them, it's probably wrong to force them elsewhere)
  1916. * - it's okay to pick a root that isn't the root of a file system, e.g.
  1917. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  1918. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  1919. * first.
  1920. */
  1921. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  1922. const char __user *, put_old)
  1923. {
  1924. struct vfsmount *tmp;
  1925. struct path new, old, parent_path, root_parent, root;
  1926. int error;
  1927. if (!capable(CAP_SYS_ADMIN))
  1928. return -EPERM;
  1929. error = user_path_dir(new_root, &new);
  1930. if (error)
  1931. goto out0;
  1932. error = -EINVAL;
  1933. if (!check_mnt(new.mnt))
  1934. goto out1;
  1935. error = user_path_dir(put_old, &old);
  1936. if (error)
  1937. goto out1;
  1938. error = security_sb_pivotroot(&old, &new);
  1939. if (error) {
  1940. path_put(&old);
  1941. goto out1;
  1942. }
  1943. get_fs_root(current->fs, &root);
  1944. down_write(&namespace_sem);
  1945. mutex_lock(&old.dentry->d_inode->i_mutex);
  1946. error = -EINVAL;
  1947. if (IS_MNT_SHARED(old.mnt) ||
  1948. IS_MNT_SHARED(new.mnt->mnt_parent) ||
  1949. IS_MNT_SHARED(root.mnt->mnt_parent))
  1950. goto out2;
  1951. if (!check_mnt(root.mnt))
  1952. goto out2;
  1953. error = -ENOENT;
  1954. if (cant_mount(old.dentry))
  1955. goto out2;
  1956. if (d_unlinked(new.dentry))
  1957. goto out2;
  1958. if (d_unlinked(old.dentry))
  1959. goto out2;
  1960. error = -EBUSY;
  1961. if (new.mnt == root.mnt ||
  1962. old.mnt == root.mnt)
  1963. goto out2; /* loop, on the same file system */
  1964. error = -EINVAL;
  1965. if (root.mnt->mnt_root != root.dentry)
  1966. goto out2; /* not a mountpoint */
  1967. if (root.mnt->mnt_parent == root.mnt)
  1968. goto out2; /* not attached */
  1969. if (new.mnt->mnt_root != new.dentry)
  1970. goto out2; /* not a mountpoint */
  1971. if (new.mnt->mnt_parent == new.mnt)
  1972. goto out2; /* not attached */
  1973. /* make sure we can reach put_old from new_root */
  1974. tmp = old.mnt;
  1975. spin_lock(&vfsmount_lock);
  1976. if (tmp != new.mnt) {
  1977. for (;;) {
  1978. if (tmp->mnt_parent == tmp)
  1979. goto out3; /* already mounted on put_old */
  1980. if (tmp->mnt_parent == new.mnt)
  1981. break;
  1982. tmp = tmp->mnt_parent;
  1983. }
  1984. if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
  1985. goto out3;
  1986. } else if (!is_subdir(old.dentry, new.dentry))
  1987. goto out3;
  1988. detach_mnt(new.mnt, &parent_path);
  1989. detach_mnt(root.mnt, &root_parent);
  1990. /* mount old root on put_old */
  1991. attach_mnt(root.mnt, &old);
  1992. /* mount new_root on / */
  1993. attach_mnt(new.mnt, &root_parent);
  1994. touch_mnt_namespace(current->nsproxy->mnt_ns);
  1995. spin_unlock(&vfsmount_lock);
  1996. chroot_fs_refs(&root, &new);
  1997. error = 0;
  1998. path_put(&root_parent);
  1999. path_put(&parent_path);
  2000. out2:
  2001. mutex_unlock(&old.dentry->d_inode->i_mutex);
  2002. up_write(&namespace_sem);
  2003. path_put(&root);
  2004. path_put(&old);
  2005. out1:
  2006. path_put(&new);
  2007. out0:
  2008. return error;
  2009. out3:
  2010. spin_unlock(&vfsmount_lock);
  2011. goto out2;
  2012. }
  2013. static void __init init_mount_tree(void)
  2014. {
  2015. struct vfsmount *mnt;
  2016. struct mnt_namespace *ns;
  2017. struct path root;
  2018. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  2019. if (IS_ERR(mnt))
  2020. panic("Can't create rootfs");
  2021. ns = create_mnt_ns(mnt);
  2022. if (IS_ERR(ns))
  2023. panic("Can't allocate initial namespace");
  2024. init_task.nsproxy->mnt_ns = ns;
  2025. get_mnt_ns(ns);
  2026. root.mnt = ns->root;
  2027. root.dentry = ns->root->mnt_root;
  2028. set_fs_pwd(current->fs, &root);
  2029. set_fs_root(current->fs, &root);
  2030. }
  2031. void __init mnt_init(void)
  2032. {
  2033. unsigned u;
  2034. int err;
  2035. init_rwsem(&namespace_sem);
  2036. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
  2037. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2038. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  2039. if (!mount_hashtable)
  2040. panic("Failed to allocate mount hash table\n");
  2041. printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
  2042. for (u = 0; u < HASH_SIZE; u++)
  2043. INIT_LIST_HEAD(&mount_hashtable[u]);
  2044. err = sysfs_init();
  2045. if (err)
  2046. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2047. __func__, err);
  2048. fs_kobj = kobject_create_and_add("fs", NULL);
  2049. if (!fs_kobj)
  2050. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2051. init_rootfs();
  2052. init_mount_tree();
  2053. }
  2054. void put_mnt_ns(struct mnt_namespace *ns)
  2055. {
  2056. LIST_HEAD(umount_list);
  2057. if (!atomic_dec_and_test(&ns->count))
  2058. return;
  2059. down_write(&namespace_sem);
  2060. spin_lock(&vfsmount_lock);
  2061. umount_tree(ns->root, 0, &umount_list);
  2062. spin_unlock(&vfsmount_lock);
  2063. up_write(&namespace_sem);
  2064. release_mounts(&umount_list);
  2065. kfree(ns);
  2066. }
  2067. EXPORT_SYMBOL(put_mnt_ns);