namespace.c 56 KB

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