namespace.c 63 KB

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