shmem.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2005 Hugh Dickins.
  10. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  11. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  12. *
  13. * Extended attribute support for tmpfs:
  14. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  15. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  16. *
  17. * This file is released under the GPL.
  18. */
  19. /*
  20. * This virtual memory filesystem is heavily based on the ramfs. It
  21. * extends ramfs by the ability to use swap and honor resource limits
  22. * which makes it a completely usable filesystem.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/fs.h>
  27. #include <linux/mm.h>
  28. #include <linux/mman.h>
  29. #include <linux/file.h>
  30. #include <linux/swap.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/string.h>
  33. #include <linux/slab.h>
  34. #include <linux/backing-dev.h>
  35. #include <linux/shmem_fs.h>
  36. #include <linux/mount.h>
  37. #include <linux/writeback.h>
  38. #include <linux/vfs.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/security.h>
  41. #include <linux/swapops.h>
  42. #include <linux/mempolicy.h>
  43. #include <linux/namei.h>
  44. #include <linux/ctype.h>
  45. #include <linux/migrate.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/div64.h>
  48. #include <asm/pgtable.h>
  49. /* This magic number is used in glibc for posix shared memory */
  50. #define TMPFS_MAGIC 0x01021994
  51. #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
  52. #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
  53. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  54. #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
  55. #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
  56. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  57. /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
  58. #define SHMEM_PAGEIN VM_READ
  59. #define SHMEM_TRUNCATE VM_WRITE
  60. /* Definition to limit shmem_truncate's steps between cond_rescheds */
  61. #define LATENCY_LIMIT 64
  62. /* Pretend that each entry is of this size in directory's i_size */
  63. #define BOGO_DIRENT_SIZE 20
  64. /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
  65. enum sgp_type {
  66. SGP_QUICK, /* don't try more than file page cache lookup */
  67. SGP_READ, /* don't exceed i_size, don't allocate page */
  68. SGP_CACHE, /* don't exceed i_size, may allocate page */
  69. SGP_WRITE, /* may exceed i_size, may allocate page */
  70. };
  71. static int shmem_getpage(struct inode *inode, unsigned long idx,
  72. struct page **pagep, enum sgp_type sgp, int *type);
  73. static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
  74. {
  75. /*
  76. * The above definition of ENTRIES_PER_PAGE, and the use of
  77. * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
  78. * might be reconsidered if it ever diverges from PAGE_SIZE.
  79. */
  80. return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  81. }
  82. static inline void shmem_dir_free(struct page *page)
  83. {
  84. __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
  85. }
  86. static struct page **shmem_dir_map(struct page *page)
  87. {
  88. return (struct page **)kmap_atomic(page, KM_USER0);
  89. }
  90. static inline void shmem_dir_unmap(struct page **dir)
  91. {
  92. kunmap_atomic(dir, KM_USER0);
  93. }
  94. static swp_entry_t *shmem_swp_map(struct page *page)
  95. {
  96. return (swp_entry_t *)kmap_atomic(page, KM_USER1);
  97. }
  98. static inline void shmem_swp_balance_unmap(void)
  99. {
  100. /*
  101. * When passing a pointer to an i_direct entry, to code which
  102. * also handles indirect entries and so will shmem_swp_unmap,
  103. * we must arrange for the preempt count to remain in balance.
  104. * What kmap_atomic of a lowmem page does depends on config
  105. * and architecture, so pretend to kmap_atomic some lowmem page.
  106. */
  107. (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
  108. }
  109. static inline void shmem_swp_unmap(swp_entry_t *entry)
  110. {
  111. kunmap_atomic(entry, KM_USER1);
  112. }
  113. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  114. {
  115. return sb->s_fs_info;
  116. }
  117. /*
  118. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  119. * for shared memory and for shared anonymous (/dev/zero) mappings
  120. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  121. * consistent with the pre-accounting of private mappings ...
  122. */
  123. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  124. {
  125. return (flags & VM_ACCOUNT)?
  126. security_vm_enough_memory(VM_ACCT(size)): 0;
  127. }
  128. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  129. {
  130. if (flags & VM_ACCOUNT)
  131. vm_unacct_memory(VM_ACCT(size));
  132. }
  133. /*
  134. * ... whereas tmpfs objects are accounted incrementally as
  135. * pages are allocated, in order to allow huge sparse files.
  136. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  137. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  138. */
  139. static inline int shmem_acct_block(unsigned long flags)
  140. {
  141. return (flags & VM_ACCOUNT)?
  142. 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
  143. }
  144. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  145. {
  146. if (!(flags & VM_ACCOUNT))
  147. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  148. }
  149. static struct super_operations shmem_ops;
  150. static const struct address_space_operations shmem_aops;
  151. static struct file_operations shmem_file_operations;
  152. static struct inode_operations shmem_inode_operations;
  153. static struct inode_operations shmem_dir_inode_operations;
  154. static struct vm_operations_struct shmem_vm_ops;
  155. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  156. .ra_pages = 0, /* No readahead */
  157. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  158. .unplug_io_fn = default_unplug_io_fn,
  159. };
  160. static LIST_HEAD(shmem_swaplist);
  161. static DEFINE_SPINLOCK(shmem_swaplist_lock);
  162. static void shmem_free_blocks(struct inode *inode, long pages)
  163. {
  164. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  165. if (sbinfo->max_blocks) {
  166. spin_lock(&sbinfo->stat_lock);
  167. sbinfo->free_blocks += pages;
  168. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  169. spin_unlock(&sbinfo->stat_lock);
  170. }
  171. }
  172. /*
  173. * shmem_recalc_inode - recalculate the size of an inode
  174. *
  175. * @inode: inode to recalc
  176. *
  177. * We have to calculate the free blocks since the mm can drop
  178. * undirtied hole pages behind our back.
  179. *
  180. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  181. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  182. *
  183. * It has to be called with the spinlock held.
  184. */
  185. static void shmem_recalc_inode(struct inode *inode)
  186. {
  187. struct shmem_inode_info *info = SHMEM_I(inode);
  188. long freed;
  189. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  190. if (freed > 0) {
  191. info->alloced -= freed;
  192. shmem_unacct_blocks(info->flags, freed);
  193. shmem_free_blocks(inode, freed);
  194. }
  195. }
  196. /*
  197. * shmem_swp_entry - find the swap vector position in the info structure
  198. *
  199. * @info: info structure for the inode
  200. * @index: index of the page to find
  201. * @page: optional page to add to the structure. Has to be preset to
  202. * all zeros
  203. *
  204. * If there is no space allocated yet it will return NULL when
  205. * page is NULL, else it will use the page for the needed block,
  206. * setting it to NULL on return to indicate that it has been used.
  207. *
  208. * The swap vector is organized the following way:
  209. *
  210. * There are SHMEM_NR_DIRECT entries directly stored in the
  211. * shmem_inode_info structure. So small files do not need an addional
  212. * allocation.
  213. *
  214. * For pages with index > SHMEM_NR_DIRECT there is the pointer
  215. * i_indirect which points to a page which holds in the first half
  216. * doubly indirect blocks, in the second half triple indirect blocks:
  217. *
  218. * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
  219. * following layout (for SHMEM_NR_DIRECT == 16):
  220. *
  221. * i_indirect -> dir --> 16-19
  222. * | +-> 20-23
  223. * |
  224. * +-->dir2 --> 24-27
  225. * | +-> 28-31
  226. * | +-> 32-35
  227. * | +-> 36-39
  228. * |
  229. * +-->dir3 --> 40-43
  230. * +-> 44-47
  231. * +-> 48-51
  232. * +-> 52-55
  233. */
  234. static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
  235. {
  236. unsigned long offset;
  237. struct page **dir;
  238. struct page *subdir;
  239. if (index < SHMEM_NR_DIRECT) {
  240. shmem_swp_balance_unmap();
  241. return info->i_direct+index;
  242. }
  243. if (!info->i_indirect) {
  244. if (page) {
  245. info->i_indirect = *page;
  246. *page = NULL;
  247. }
  248. return NULL; /* need another page */
  249. }
  250. index -= SHMEM_NR_DIRECT;
  251. offset = index % ENTRIES_PER_PAGE;
  252. index /= ENTRIES_PER_PAGE;
  253. dir = shmem_dir_map(info->i_indirect);
  254. if (index >= ENTRIES_PER_PAGE/2) {
  255. index -= ENTRIES_PER_PAGE/2;
  256. dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
  257. index %= ENTRIES_PER_PAGE;
  258. subdir = *dir;
  259. if (!subdir) {
  260. if (page) {
  261. *dir = *page;
  262. *page = NULL;
  263. }
  264. shmem_dir_unmap(dir);
  265. return NULL; /* need another page */
  266. }
  267. shmem_dir_unmap(dir);
  268. dir = shmem_dir_map(subdir);
  269. }
  270. dir += index;
  271. subdir = *dir;
  272. if (!subdir) {
  273. if (!page || !(subdir = *page)) {
  274. shmem_dir_unmap(dir);
  275. return NULL; /* need a page */
  276. }
  277. *dir = subdir;
  278. *page = NULL;
  279. }
  280. shmem_dir_unmap(dir);
  281. return shmem_swp_map(subdir) + offset;
  282. }
  283. static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
  284. {
  285. long incdec = value? 1: -1;
  286. entry->val = value;
  287. info->swapped += incdec;
  288. if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
  289. struct page *page = kmap_atomic_to_page(entry);
  290. set_page_private(page, page_private(page) + incdec);
  291. }
  292. }
  293. /*
  294. * shmem_swp_alloc - get the position of the swap entry for the page.
  295. * If it does not exist allocate the entry.
  296. *
  297. * @info: info structure for the inode
  298. * @index: index of the page to find
  299. * @sgp: check and recheck i_size? skip allocation?
  300. */
  301. static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
  302. {
  303. struct inode *inode = &info->vfs_inode;
  304. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  305. struct page *page = NULL;
  306. swp_entry_t *entry;
  307. if (sgp != SGP_WRITE &&
  308. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  309. return ERR_PTR(-EINVAL);
  310. while (!(entry = shmem_swp_entry(info, index, &page))) {
  311. if (sgp == SGP_READ)
  312. return shmem_swp_map(ZERO_PAGE(0));
  313. /*
  314. * Test free_blocks against 1 not 0, since we have 1 data
  315. * page (and perhaps indirect index pages) yet to allocate:
  316. * a waste to allocate index if we cannot allocate data.
  317. */
  318. if (sbinfo->max_blocks) {
  319. spin_lock(&sbinfo->stat_lock);
  320. if (sbinfo->free_blocks <= 1) {
  321. spin_unlock(&sbinfo->stat_lock);
  322. return ERR_PTR(-ENOSPC);
  323. }
  324. sbinfo->free_blocks--;
  325. inode->i_blocks += BLOCKS_PER_PAGE;
  326. spin_unlock(&sbinfo->stat_lock);
  327. }
  328. spin_unlock(&info->lock);
  329. page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping) | __GFP_ZERO);
  330. if (page)
  331. set_page_private(page, 0);
  332. spin_lock(&info->lock);
  333. if (!page) {
  334. shmem_free_blocks(inode, 1);
  335. return ERR_PTR(-ENOMEM);
  336. }
  337. if (sgp != SGP_WRITE &&
  338. ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  339. entry = ERR_PTR(-EINVAL);
  340. break;
  341. }
  342. if (info->next_index <= index)
  343. info->next_index = index + 1;
  344. }
  345. if (page) {
  346. /* another task gave its page, or truncated the file */
  347. shmem_free_blocks(inode, 1);
  348. shmem_dir_free(page);
  349. }
  350. if (info->next_index <= index && !IS_ERR(entry))
  351. info->next_index = index + 1;
  352. return entry;
  353. }
  354. /*
  355. * shmem_free_swp - free some swap entries in a directory
  356. *
  357. * @dir: pointer to the directory
  358. * @edir: pointer after last entry of the directory
  359. */
  360. static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir)
  361. {
  362. swp_entry_t *ptr;
  363. int freed = 0;
  364. for (ptr = dir; ptr < edir; ptr++) {
  365. if (ptr->val) {
  366. free_swap_and_cache(*ptr);
  367. *ptr = (swp_entry_t){0};
  368. freed++;
  369. }
  370. }
  371. return freed;
  372. }
  373. static int shmem_map_and_free_swp(struct page *subdir,
  374. int offset, int limit, struct page ***dir)
  375. {
  376. swp_entry_t *ptr;
  377. int freed = 0;
  378. ptr = shmem_swp_map(subdir);
  379. for (; offset < limit; offset += LATENCY_LIMIT) {
  380. int size = limit - offset;
  381. if (size > LATENCY_LIMIT)
  382. size = LATENCY_LIMIT;
  383. freed += shmem_free_swp(ptr+offset, ptr+offset+size);
  384. if (need_resched()) {
  385. shmem_swp_unmap(ptr);
  386. if (*dir) {
  387. shmem_dir_unmap(*dir);
  388. *dir = NULL;
  389. }
  390. cond_resched();
  391. ptr = shmem_swp_map(subdir);
  392. }
  393. }
  394. shmem_swp_unmap(ptr);
  395. return freed;
  396. }
  397. static void shmem_free_pages(struct list_head *next)
  398. {
  399. struct page *page;
  400. int freed = 0;
  401. do {
  402. page = container_of(next, struct page, lru);
  403. next = next->next;
  404. shmem_dir_free(page);
  405. freed++;
  406. if (freed >= LATENCY_LIMIT) {
  407. cond_resched();
  408. freed = 0;
  409. }
  410. } while (next);
  411. }
  412. static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
  413. {
  414. struct shmem_inode_info *info = SHMEM_I(inode);
  415. unsigned long idx;
  416. unsigned long size;
  417. unsigned long limit;
  418. unsigned long stage;
  419. unsigned long diroff;
  420. struct page **dir;
  421. struct page *topdir;
  422. struct page *middir;
  423. struct page *subdir;
  424. swp_entry_t *ptr;
  425. LIST_HEAD(pages_to_free);
  426. long nr_pages_to_free = 0;
  427. long nr_swaps_freed = 0;
  428. int offset;
  429. int freed;
  430. int punch_hole = 0;
  431. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  432. idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  433. if (idx >= info->next_index)
  434. return;
  435. spin_lock(&info->lock);
  436. info->flags |= SHMEM_TRUNCATE;
  437. if (likely(end == (loff_t) -1)) {
  438. limit = info->next_index;
  439. info->next_index = idx;
  440. } else {
  441. limit = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  442. if (limit > info->next_index)
  443. limit = info->next_index;
  444. punch_hole = 1;
  445. }
  446. topdir = info->i_indirect;
  447. if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
  448. info->i_indirect = NULL;
  449. nr_pages_to_free++;
  450. list_add(&topdir->lru, &pages_to_free);
  451. }
  452. spin_unlock(&info->lock);
  453. if (info->swapped && idx < SHMEM_NR_DIRECT) {
  454. ptr = info->i_direct;
  455. size = limit;
  456. if (size > SHMEM_NR_DIRECT)
  457. size = SHMEM_NR_DIRECT;
  458. nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size);
  459. }
  460. if (!topdir)
  461. goto done2;
  462. BUG_ON(limit <= SHMEM_NR_DIRECT);
  463. limit -= SHMEM_NR_DIRECT;
  464. idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
  465. offset = idx % ENTRIES_PER_PAGE;
  466. idx -= offset;
  467. dir = shmem_dir_map(topdir);
  468. stage = ENTRIES_PER_PAGEPAGE/2;
  469. if (idx < ENTRIES_PER_PAGEPAGE/2) {
  470. middir = topdir;
  471. diroff = idx/ENTRIES_PER_PAGE;
  472. } else {
  473. dir += ENTRIES_PER_PAGE/2;
  474. dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
  475. while (stage <= idx)
  476. stage += ENTRIES_PER_PAGEPAGE;
  477. middir = *dir;
  478. if (*dir) {
  479. diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
  480. ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
  481. if (!diroff && !offset) {
  482. *dir = NULL;
  483. nr_pages_to_free++;
  484. list_add(&middir->lru, &pages_to_free);
  485. }
  486. shmem_dir_unmap(dir);
  487. dir = shmem_dir_map(middir);
  488. } else {
  489. diroff = 0;
  490. offset = 0;
  491. idx = stage;
  492. }
  493. }
  494. for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
  495. if (unlikely(idx == stage)) {
  496. shmem_dir_unmap(dir);
  497. dir = shmem_dir_map(topdir) +
  498. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  499. while (!*dir) {
  500. dir++;
  501. idx += ENTRIES_PER_PAGEPAGE;
  502. if (idx >= limit)
  503. goto done1;
  504. }
  505. stage = idx + ENTRIES_PER_PAGEPAGE;
  506. middir = *dir;
  507. *dir = NULL;
  508. nr_pages_to_free++;
  509. list_add(&middir->lru, &pages_to_free);
  510. shmem_dir_unmap(dir);
  511. cond_resched();
  512. dir = shmem_dir_map(middir);
  513. diroff = 0;
  514. }
  515. subdir = dir[diroff];
  516. if (subdir && page_private(subdir)) {
  517. size = limit - idx;
  518. if (size > ENTRIES_PER_PAGE)
  519. size = ENTRIES_PER_PAGE;
  520. freed = shmem_map_and_free_swp(subdir,
  521. offset, size, &dir);
  522. if (!dir)
  523. dir = shmem_dir_map(middir);
  524. nr_swaps_freed += freed;
  525. if (offset)
  526. spin_lock(&info->lock);
  527. set_page_private(subdir, page_private(subdir) - freed);
  528. if (offset)
  529. spin_unlock(&info->lock);
  530. if (!punch_hole)
  531. BUG_ON(page_private(subdir) > offset);
  532. }
  533. if (offset)
  534. offset = 0;
  535. else if (subdir && !page_private(subdir)) {
  536. dir[diroff] = NULL;
  537. nr_pages_to_free++;
  538. list_add(&subdir->lru, &pages_to_free);
  539. }
  540. }
  541. done1:
  542. shmem_dir_unmap(dir);
  543. done2:
  544. if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
  545. /*
  546. * Call truncate_inode_pages again: racing shmem_unuse_inode
  547. * may have swizzled a page in from swap since vmtruncate or
  548. * generic_delete_inode did it, before we lowered next_index.
  549. * Also, though shmem_getpage checks i_size before adding to
  550. * cache, no recheck after: so fix the narrow window there too.
  551. */
  552. truncate_inode_pages_range(inode->i_mapping, start, end);
  553. }
  554. spin_lock(&info->lock);
  555. info->flags &= ~SHMEM_TRUNCATE;
  556. info->swapped -= nr_swaps_freed;
  557. if (nr_pages_to_free)
  558. shmem_free_blocks(inode, nr_pages_to_free);
  559. shmem_recalc_inode(inode);
  560. spin_unlock(&info->lock);
  561. /*
  562. * Empty swap vector directory pages to be freed?
  563. */
  564. if (!list_empty(&pages_to_free)) {
  565. pages_to_free.prev->next = NULL;
  566. shmem_free_pages(pages_to_free.next);
  567. }
  568. }
  569. static void shmem_truncate(struct inode *inode)
  570. {
  571. shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
  572. }
  573. static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
  574. {
  575. struct inode *inode = dentry->d_inode;
  576. struct page *page = NULL;
  577. int error;
  578. if (attr->ia_valid & ATTR_SIZE) {
  579. if (attr->ia_size < inode->i_size) {
  580. /*
  581. * If truncating down to a partial page, then
  582. * if that page is already allocated, hold it
  583. * in memory until the truncation is over, so
  584. * truncate_partial_page cannnot miss it were
  585. * it assigned to swap.
  586. */
  587. if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
  588. (void) shmem_getpage(inode,
  589. attr->ia_size>>PAGE_CACHE_SHIFT,
  590. &page, SGP_READ, NULL);
  591. }
  592. /*
  593. * Reset SHMEM_PAGEIN flag so that shmem_truncate can
  594. * detect if any pages might have been added to cache
  595. * after truncate_inode_pages. But we needn't bother
  596. * if it's being fully truncated to zero-length: the
  597. * nrpages check is efficient enough in that case.
  598. */
  599. if (attr->ia_size) {
  600. struct shmem_inode_info *info = SHMEM_I(inode);
  601. spin_lock(&info->lock);
  602. info->flags &= ~SHMEM_PAGEIN;
  603. spin_unlock(&info->lock);
  604. }
  605. }
  606. }
  607. error = inode_change_ok(inode, attr);
  608. if (!error)
  609. error = inode_setattr(inode, attr);
  610. if (page)
  611. page_cache_release(page);
  612. return error;
  613. }
  614. static void shmem_delete_inode(struct inode *inode)
  615. {
  616. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  617. struct shmem_inode_info *info = SHMEM_I(inode);
  618. if (inode->i_op->truncate == shmem_truncate) {
  619. truncate_inode_pages(inode->i_mapping, 0);
  620. shmem_unacct_size(info->flags, inode->i_size);
  621. inode->i_size = 0;
  622. shmem_truncate(inode);
  623. if (!list_empty(&info->swaplist)) {
  624. spin_lock(&shmem_swaplist_lock);
  625. list_del_init(&info->swaplist);
  626. spin_unlock(&shmem_swaplist_lock);
  627. }
  628. }
  629. BUG_ON(inode->i_blocks);
  630. if (sbinfo->max_inodes) {
  631. spin_lock(&sbinfo->stat_lock);
  632. sbinfo->free_inodes++;
  633. spin_unlock(&sbinfo->stat_lock);
  634. }
  635. clear_inode(inode);
  636. }
  637. static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
  638. {
  639. swp_entry_t *ptr;
  640. for (ptr = dir; ptr < edir; ptr++) {
  641. if (ptr->val == entry.val)
  642. return ptr - dir;
  643. }
  644. return -1;
  645. }
  646. static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
  647. {
  648. struct inode *inode;
  649. unsigned long idx;
  650. unsigned long size;
  651. unsigned long limit;
  652. unsigned long stage;
  653. struct page **dir;
  654. struct page *subdir;
  655. swp_entry_t *ptr;
  656. int offset;
  657. idx = 0;
  658. ptr = info->i_direct;
  659. spin_lock(&info->lock);
  660. limit = info->next_index;
  661. size = limit;
  662. if (size > SHMEM_NR_DIRECT)
  663. size = SHMEM_NR_DIRECT;
  664. offset = shmem_find_swp(entry, ptr, ptr+size);
  665. if (offset >= 0) {
  666. shmem_swp_balance_unmap();
  667. goto found;
  668. }
  669. if (!info->i_indirect)
  670. goto lost2;
  671. dir = shmem_dir_map(info->i_indirect);
  672. stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
  673. for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
  674. if (unlikely(idx == stage)) {
  675. shmem_dir_unmap(dir-1);
  676. dir = shmem_dir_map(info->i_indirect) +
  677. ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
  678. while (!*dir) {
  679. dir++;
  680. idx += ENTRIES_PER_PAGEPAGE;
  681. if (idx >= limit)
  682. goto lost1;
  683. }
  684. stage = idx + ENTRIES_PER_PAGEPAGE;
  685. subdir = *dir;
  686. shmem_dir_unmap(dir);
  687. dir = shmem_dir_map(subdir);
  688. }
  689. subdir = *dir;
  690. if (subdir && page_private(subdir)) {
  691. ptr = shmem_swp_map(subdir);
  692. size = limit - idx;
  693. if (size > ENTRIES_PER_PAGE)
  694. size = ENTRIES_PER_PAGE;
  695. offset = shmem_find_swp(entry, ptr, ptr+size);
  696. if (offset >= 0) {
  697. shmem_dir_unmap(dir);
  698. goto found;
  699. }
  700. shmem_swp_unmap(ptr);
  701. }
  702. }
  703. lost1:
  704. shmem_dir_unmap(dir-1);
  705. lost2:
  706. spin_unlock(&info->lock);
  707. return 0;
  708. found:
  709. idx += offset;
  710. inode = &info->vfs_inode;
  711. if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
  712. info->flags |= SHMEM_PAGEIN;
  713. shmem_swp_set(info, ptr + offset, 0);
  714. }
  715. shmem_swp_unmap(ptr);
  716. spin_unlock(&info->lock);
  717. /*
  718. * Decrement swap count even when the entry is left behind:
  719. * try_to_unuse will skip over mms, then reincrement count.
  720. */
  721. swap_free(entry);
  722. return 1;
  723. }
  724. /*
  725. * shmem_unuse() search for an eventually swapped out shmem page.
  726. */
  727. int shmem_unuse(swp_entry_t entry, struct page *page)
  728. {
  729. struct list_head *p, *next;
  730. struct shmem_inode_info *info;
  731. int found = 0;
  732. spin_lock(&shmem_swaplist_lock);
  733. list_for_each_safe(p, next, &shmem_swaplist) {
  734. info = list_entry(p, struct shmem_inode_info, swaplist);
  735. if (!info->swapped)
  736. list_del_init(&info->swaplist);
  737. else if (shmem_unuse_inode(info, entry, page)) {
  738. /* move head to start search for next from here */
  739. list_move_tail(&shmem_swaplist, &info->swaplist);
  740. found = 1;
  741. break;
  742. }
  743. }
  744. spin_unlock(&shmem_swaplist_lock);
  745. return found;
  746. }
  747. /*
  748. * Move the page from the page cache to the swap cache.
  749. */
  750. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  751. {
  752. struct shmem_inode_info *info;
  753. swp_entry_t *entry, swap;
  754. struct address_space *mapping;
  755. unsigned long index;
  756. struct inode *inode;
  757. BUG_ON(!PageLocked(page));
  758. BUG_ON(page_mapped(page));
  759. mapping = page->mapping;
  760. index = page->index;
  761. inode = mapping->host;
  762. info = SHMEM_I(inode);
  763. if (info->flags & VM_LOCKED)
  764. goto redirty;
  765. swap = get_swap_page();
  766. if (!swap.val)
  767. goto redirty;
  768. spin_lock(&info->lock);
  769. shmem_recalc_inode(inode);
  770. if (index >= info->next_index) {
  771. BUG_ON(!(info->flags & SHMEM_TRUNCATE));
  772. goto unlock;
  773. }
  774. entry = shmem_swp_entry(info, index, NULL);
  775. BUG_ON(!entry);
  776. BUG_ON(entry->val);
  777. if (move_to_swap_cache(page, swap) == 0) {
  778. shmem_swp_set(info, entry, swap.val);
  779. shmem_swp_unmap(entry);
  780. spin_unlock(&info->lock);
  781. if (list_empty(&info->swaplist)) {
  782. spin_lock(&shmem_swaplist_lock);
  783. /* move instead of add in case we're racing */
  784. list_move_tail(&info->swaplist, &shmem_swaplist);
  785. spin_unlock(&shmem_swaplist_lock);
  786. }
  787. unlock_page(page);
  788. return 0;
  789. }
  790. shmem_swp_unmap(entry);
  791. unlock:
  792. spin_unlock(&info->lock);
  793. swap_free(swap);
  794. redirty:
  795. set_page_dirty(page);
  796. return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
  797. }
  798. #ifdef CONFIG_NUMA
  799. static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
  800. {
  801. char *nodelist = strchr(value, ':');
  802. int err = 1;
  803. if (nodelist) {
  804. /* NUL-terminate policy string */
  805. *nodelist++ = '\0';
  806. if (nodelist_parse(nodelist, *policy_nodes))
  807. goto out;
  808. }
  809. if (!strcmp(value, "default")) {
  810. *policy = MPOL_DEFAULT;
  811. /* Don't allow a nodelist */
  812. if (!nodelist)
  813. err = 0;
  814. } else if (!strcmp(value, "prefer")) {
  815. *policy = MPOL_PREFERRED;
  816. /* Insist on a nodelist of one node only */
  817. if (nodelist) {
  818. char *rest = nodelist;
  819. while (isdigit(*rest))
  820. rest++;
  821. if (!*rest)
  822. err = 0;
  823. }
  824. } else if (!strcmp(value, "bind")) {
  825. *policy = MPOL_BIND;
  826. /* Insist on a nodelist */
  827. if (nodelist)
  828. err = 0;
  829. } else if (!strcmp(value, "interleave")) {
  830. *policy = MPOL_INTERLEAVE;
  831. /* Default to nodes online if no nodelist */
  832. if (!nodelist)
  833. *policy_nodes = node_online_map;
  834. err = 0;
  835. }
  836. out:
  837. /* Restore string for error message */
  838. if (nodelist)
  839. *--nodelist = ':';
  840. return err;
  841. }
  842. static struct page *shmem_swapin_async(struct shared_policy *p,
  843. swp_entry_t entry, unsigned long idx)
  844. {
  845. struct page *page;
  846. struct vm_area_struct pvma;
  847. /* Create a pseudo vma that just contains the policy */
  848. memset(&pvma, 0, sizeof(struct vm_area_struct));
  849. pvma.vm_end = PAGE_SIZE;
  850. pvma.vm_pgoff = idx;
  851. pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
  852. page = read_swap_cache_async(entry, &pvma, 0);
  853. mpol_free(pvma.vm_policy);
  854. return page;
  855. }
  856. struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
  857. unsigned long idx)
  858. {
  859. struct shared_policy *p = &info->policy;
  860. int i, num;
  861. struct page *page;
  862. unsigned long offset;
  863. num = valid_swaphandles(entry, &offset);
  864. for (i = 0; i < num; offset++, i++) {
  865. page = shmem_swapin_async(p,
  866. swp_entry(swp_type(entry), offset), idx);
  867. if (!page)
  868. break;
  869. page_cache_release(page);
  870. }
  871. lru_add_drain(); /* Push any new pages onto the LRU now */
  872. return shmem_swapin_async(p, entry, idx);
  873. }
  874. static struct page *
  875. shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
  876. unsigned long idx)
  877. {
  878. struct vm_area_struct pvma;
  879. struct page *page;
  880. memset(&pvma, 0, sizeof(struct vm_area_struct));
  881. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  882. pvma.vm_pgoff = idx;
  883. pvma.vm_end = PAGE_SIZE;
  884. page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
  885. mpol_free(pvma.vm_policy);
  886. return page;
  887. }
  888. #else
  889. static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
  890. {
  891. return 1;
  892. }
  893. static inline struct page *
  894. shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
  895. {
  896. swapin_readahead(entry, 0, NULL);
  897. return read_swap_cache_async(entry, NULL, 0);
  898. }
  899. static inline struct page *
  900. shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
  901. {
  902. return alloc_page(gfp | __GFP_ZERO);
  903. }
  904. #endif
  905. /*
  906. * shmem_getpage - either get the page from swap or allocate a new one
  907. *
  908. * If we allocate a new one we do not mark it dirty. That's up to the
  909. * vm. If we swap it in we mark it dirty since we also free the swap
  910. * entry since a page cannot live in both the swap and page cache
  911. */
  912. static int shmem_getpage(struct inode *inode, unsigned long idx,
  913. struct page **pagep, enum sgp_type sgp, int *type)
  914. {
  915. struct address_space *mapping = inode->i_mapping;
  916. struct shmem_inode_info *info = SHMEM_I(inode);
  917. struct shmem_sb_info *sbinfo;
  918. struct page *filepage = *pagep;
  919. struct page *swappage;
  920. swp_entry_t *entry;
  921. swp_entry_t swap;
  922. int error;
  923. if (idx >= SHMEM_MAX_INDEX)
  924. return -EFBIG;
  925. /*
  926. * Normally, filepage is NULL on entry, and either found
  927. * uptodate immediately, or allocated and zeroed, or read
  928. * in under swappage, which is then assigned to filepage.
  929. * But shmem_prepare_write passes in a locked filepage,
  930. * which may be found not uptodate by other callers too,
  931. * and may need to be copied from the swappage read in.
  932. */
  933. repeat:
  934. if (!filepage)
  935. filepage = find_lock_page(mapping, idx);
  936. if (filepage && PageUptodate(filepage))
  937. goto done;
  938. error = 0;
  939. if (sgp == SGP_QUICK)
  940. goto failed;
  941. spin_lock(&info->lock);
  942. shmem_recalc_inode(inode);
  943. entry = shmem_swp_alloc(info, idx, sgp);
  944. if (IS_ERR(entry)) {
  945. spin_unlock(&info->lock);
  946. error = PTR_ERR(entry);
  947. goto failed;
  948. }
  949. swap = *entry;
  950. if (swap.val) {
  951. /* Look it up and read it in.. */
  952. swappage = lookup_swap_cache(swap);
  953. if (!swappage) {
  954. shmem_swp_unmap(entry);
  955. /* here we actually do the io */
  956. if (type && *type == VM_FAULT_MINOR) {
  957. __count_vm_event(PGMAJFAULT);
  958. *type = VM_FAULT_MAJOR;
  959. }
  960. spin_unlock(&info->lock);
  961. swappage = shmem_swapin(info, swap, idx);
  962. if (!swappage) {
  963. spin_lock(&info->lock);
  964. entry = shmem_swp_alloc(info, idx, sgp);
  965. if (IS_ERR(entry))
  966. error = PTR_ERR(entry);
  967. else {
  968. if (entry->val == swap.val)
  969. error = -ENOMEM;
  970. shmem_swp_unmap(entry);
  971. }
  972. spin_unlock(&info->lock);
  973. if (error)
  974. goto failed;
  975. goto repeat;
  976. }
  977. wait_on_page_locked(swappage);
  978. page_cache_release(swappage);
  979. goto repeat;
  980. }
  981. /* We have to do this with page locked to prevent races */
  982. if (TestSetPageLocked(swappage)) {
  983. shmem_swp_unmap(entry);
  984. spin_unlock(&info->lock);
  985. wait_on_page_locked(swappage);
  986. page_cache_release(swappage);
  987. goto repeat;
  988. }
  989. if (PageWriteback(swappage)) {
  990. shmem_swp_unmap(entry);
  991. spin_unlock(&info->lock);
  992. wait_on_page_writeback(swappage);
  993. unlock_page(swappage);
  994. page_cache_release(swappage);
  995. goto repeat;
  996. }
  997. if (!PageUptodate(swappage)) {
  998. shmem_swp_unmap(entry);
  999. spin_unlock(&info->lock);
  1000. unlock_page(swappage);
  1001. page_cache_release(swappage);
  1002. error = -EIO;
  1003. goto failed;
  1004. }
  1005. if (filepage) {
  1006. shmem_swp_set(info, entry, 0);
  1007. shmem_swp_unmap(entry);
  1008. delete_from_swap_cache(swappage);
  1009. spin_unlock(&info->lock);
  1010. copy_highpage(filepage, swappage);
  1011. unlock_page(swappage);
  1012. page_cache_release(swappage);
  1013. flush_dcache_page(filepage);
  1014. SetPageUptodate(filepage);
  1015. set_page_dirty(filepage);
  1016. swap_free(swap);
  1017. } else if (!(error = move_from_swap_cache(
  1018. swappage, idx, mapping))) {
  1019. info->flags |= SHMEM_PAGEIN;
  1020. shmem_swp_set(info, entry, 0);
  1021. shmem_swp_unmap(entry);
  1022. spin_unlock(&info->lock);
  1023. filepage = swappage;
  1024. swap_free(swap);
  1025. } else {
  1026. shmem_swp_unmap(entry);
  1027. spin_unlock(&info->lock);
  1028. unlock_page(swappage);
  1029. page_cache_release(swappage);
  1030. if (error == -ENOMEM) {
  1031. /* let kswapd refresh zone for GFP_ATOMICs */
  1032. blk_congestion_wait(WRITE, HZ/50);
  1033. }
  1034. goto repeat;
  1035. }
  1036. } else if (sgp == SGP_READ && !filepage) {
  1037. shmem_swp_unmap(entry);
  1038. filepage = find_get_page(mapping, idx);
  1039. if (filepage &&
  1040. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  1041. spin_unlock(&info->lock);
  1042. wait_on_page_locked(filepage);
  1043. page_cache_release(filepage);
  1044. filepage = NULL;
  1045. goto repeat;
  1046. }
  1047. spin_unlock(&info->lock);
  1048. } else {
  1049. shmem_swp_unmap(entry);
  1050. sbinfo = SHMEM_SB(inode->i_sb);
  1051. if (sbinfo->max_blocks) {
  1052. spin_lock(&sbinfo->stat_lock);
  1053. if (sbinfo->free_blocks == 0 ||
  1054. shmem_acct_block(info->flags)) {
  1055. spin_unlock(&sbinfo->stat_lock);
  1056. spin_unlock(&info->lock);
  1057. error = -ENOSPC;
  1058. goto failed;
  1059. }
  1060. sbinfo->free_blocks--;
  1061. inode->i_blocks += BLOCKS_PER_PAGE;
  1062. spin_unlock(&sbinfo->stat_lock);
  1063. } else if (shmem_acct_block(info->flags)) {
  1064. spin_unlock(&info->lock);
  1065. error = -ENOSPC;
  1066. goto failed;
  1067. }
  1068. if (!filepage) {
  1069. spin_unlock(&info->lock);
  1070. filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
  1071. info,
  1072. idx);
  1073. if (!filepage) {
  1074. shmem_unacct_blocks(info->flags, 1);
  1075. shmem_free_blocks(inode, 1);
  1076. error = -ENOMEM;
  1077. goto failed;
  1078. }
  1079. spin_lock(&info->lock);
  1080. entry = shmem_swp_alloc(info, idx, sgp);
  1081. if (IS_ERR(entry))
  1082. error = PTR_ERR(entry);
  1083. else {
  1084. swap = *entry;
  1085. shmem_swp_unmap(entry);
  1086. }
  1087. if (error || swap.val || 0 != add_to_page_cache_lru(
  1088. filepage, mapping, idx, GFP_ATOMIC)) {
  1089. spin_unlock(&info->lock);
  1090. page_cache_release(filepage);
  1091. shmem_unacct_blocks(info->flags, 1);
  1092. shmem_free_blocks(inode, 1);
  1093. filepage = NULL;
  1094. if (error)
  1095. goto failed;
  1096. goto repeat;
  1097. }
  1098. info->flags |= SHMEM_PAGEIN;
  1099. }
  1100. info->alloced++;
  1101. spin_unlock(&info->lock);
  1102. flush_dcache_page(filepage);
  1103. SetPageUptodate(filepage);
  1104. }
  1105. done:
  1106. if (*pagep != filepage) {
  1107. unlock_page(filepage);
  1108. *pagep = filepage;
  1109. }
  1110. return 0;
  1111. failed:
  1112. if (*pagep != filepage) {
  1113. unlock_page(filepage);
  1114. page_cache_release(filepage);
  1115. }
  1116. return error;
  1117. }
  1118. struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
  1119. {
  1120. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1121. struct page *page = NULL;
  1122. unsigned long idx;
  1123. int error;
  1124. idx = (address - vma->vm_start) >> PAGE_SHIFT;
  1125. idx += vma->vm_pgoff;
  1126. idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
  1127. if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1128. return NOPAGE_SIGBUS;
  1129. error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
  1130. if (error)
  1131. return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
  1132. mark_page_accessed(page);
  1133. return page;
  1134. }
  1135. static int shmem_populate(struct vm_area_struct *vma,
  1136. unsigned long addr, unsigned long len,
  1137. pgprot_t prot, unsigned long pgoff, int nonblock)
  1138. {
  1139. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1140. struct mm_struct *mm = vma->vm_mm;
  1141. enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
  1142. unsigned long size;
  1143. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1144. if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
  1145. return -EINVAL;
  1146. while ((long) len > 0) {
  1147. struct page *page = NULL;
  1148. int err;
  1149. /*
  1150. * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
  1151. */
  1152. err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
  1153. if (err)
  1154. return err;
  1155. /* Page may still be null, but only if nonblock was set. */
  1156. if (page) {
  1157. mark_page_accessed(page);
  1158. err = install_page(mm, vma, addr, page, prot);
  1159. if (err) {
  1160. page_cache_release(page);
  1161. return err;
  1162. }
  1163. } else if (vma->vm_flags & VM_NONLINEAR) {
  1164. /* No page was found just because we can't read it in
  1165. * now (being here implies nonblock != 0), but the page
  1166. * may exist, so set the PTE to fault it in later. */
  1167. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1168. if (err)
  1169. return err;
  1170. }
  1171. len -= PAGE_SIZE;
  1172. addr += PAGE_SIZE;
  1173. pgoff++;
  1174. }
  1175. return 0;
  1176. }
  1177. #ifdef CONFIG_NUMA
  1178. int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1179. {
  1180. struct inode *i = vma->vm_file->f_dentry->d_inode;
  1181. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1182. }
  1183. struct mempolicy *
  1184. shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
  1185. {
  1186. struct inode *i = vma->vm_file->f_dentry->d_inode;
  1187. unsigned long idx;
  1188. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1189. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1190. }
  1191. #endif
  1192. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1193. {
  1194. struct inode *inode = file->f_dentry->d_inode;
  1195. struct shmem_inode_info *info = SHMEM_I(inode);
  1196. int retval = -ENOMEM;
  1197. spin_lock(&info->lock);
  1198. if (lock && !(info->flags & VM_LOCKED)) {
  1199. if (!user_shm_lock(inode->i_size, user))
  1200. goto out_nomem;
  1201. info->flags |= VM_LOCKED;
  1202. }
  1203. if (!lock && (info->flags & VM_LOCKED) && user) {
  1204. user_shm_unlock(inode->i_size, user);
  1205. info->flags &= ~VM_LOCKED;
  1206. }
  1207. retval = 0;
  1208. out_nomem:
  1209. spin_unlock(&info->lock);
  1210. return retval;
  1211. }
  1212. int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1213. {
  1214. file_accessed(file);
  1215. vma->vm_ops = &shmem_vm_ops;
  1216. return 0;
  1217. }
  1218. static struct inode *
  1219. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1220. {
  1221. struct inode *inode;
  1222. struct shmem_inode_info *info;
  1223. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1224. if (sbinfo->max_inodes) {
  1225. spin_lock(&sbinfo->stat_lock);
  1226. if (!sbinfo->free_inodes) {
  1227. spin_unlock(&sbinfo->stat_lock);
  1228. return NULL;
  1229. }
  1230. sbinfo->free_inodes--;
  1231. spin_unlock(&sbinfo->stat_lock);
  1232. }
  1233. inode = new_inode(sb);
  1234. if (inode) {
  1235. inode->i_mode = mode;
  1236. inode->i_uid = current->fsuid;
  1237. inode->i_gid = current->fsgid;
  1238. inode->i_blksize = PAGE_CACHE_SIZE;
  1239. inode->i_blocks = 0;
  1240. inode->i_mapping->a_ops = &shmem_aops;
  1241. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1242. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1243. info = SHMEM_I(inode);
  1244. memset(info, 0, (char *)inode - (char *)info);
  1245. spin_lock_init(&info->lock);
  1246. INIT_LIST_HEAD(&info->swaplist);
  1247. switch (mode & S_IFMT) {
  1248. default:
  1249. init_special_inode(inode, mode, dev);
  1250. break;
  1251. case S_IFREG:
  1252. inode->i_op = &shmem_inode_operations;
  1253. inode->i_fop = &shmem_file_operations;
  1254. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1255. &sbinfo->policy_nodes);
  1256. break;
  1257. case S_IFDIR:
  1258. inode->i_nlink++;
  1259. /* Some things misbehave if size == 0 on a directory */
  1260. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1261. inode->i_op = &shmem_dir_inode_operations;
  1262. inode->i_fop = &simple_dir_operations;
  1263. break;
  1264. case S_IFLNK:
  1265. /*
  1266. * Must not load anything in the rbtree,
  1267. * mpol_free_shared_policy will not be called.
  1268. */
  1269. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1270. NULL);
  1271. break;
  1272. }
  1273. } else if (sbinfo->max_inodes) {
  1274. spin_lock(&sbinfo->stat_lock);
  1275. sbinfo->free_inodes++;
  1276. spin_unlock(&sbinfo->stat_lock);
  1277. }
  1278. return inode;
  1279. }
  1280. #ifdef CONFIG_TMPFS
  1281. static struct inode_operations shmem_symlink_inode_operations;
  1282. static struct inode_operations shmem_symlink_inline_operations;
  1283. /*
  1284. * Normally tmpfs makes no use of shmem_prepare_write, but it
  1285. * lets a tmpfs file be used read-write below the loop driver.
  1286. */
  1287. static int
  1288. shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  1289. {
  1290. struct inode *inode = page->mapping->host;
  1291. return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
  1292. }
  1293. static ssize_t
  1294. shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  1295. {
  1296. struct inode *inode = file->f_dentry->d_inode;
  1297. loff_t pos;
  1298. unsigned long written;
  1299. ssize_t err;
  1300. if ((ssize_t) count < 0)
  1301. return -EINVAL;
  1302. if (!access_ok(VERIFY_READ, buf, count))
  1303. return -EFAULT;
  1304. mutex_lock(&inode->i_mutex);
  1305. pos = *ppos;
  1306. written = 0;
  1307. err = generic_write_checks(file, &pos, &count, 0);
  1308. if (err || !count)
  1309. goto out;
  1310. err = remove_suid(file->f_dentry);
  1311. if (err)
  1312. goto out;
  1313. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1314. do {
  1315. struct page *page = NULL;
  1316. unsigned long bytes, index, offset;
  1317. char *kaddr;
  1318. int left;
  1319. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1320. index = pos >> PAGE_CACHE_SHIFT;
  1321. bytes = PAGE_CACHE_SIZE - offset;
  1322. if (bytes > count)
  1323. bytes = count;
  1324. /*
  1325. * We don't hold page lock across copy from user -
  1326. * what would it guard against? - so no deadlock here.
  1327. * But it still may be a good idea to prefault below.
  1328. */
  1329. err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
  1330. if (err)
  1331. break;
  1332. left = bytes;
  1333. if (PageHighMem(page)) {
  1334. volatile unsigned char dummy;
  1335. __get_user(dummy, buf);
  1336. __get_user(dummy, buf + bytes - 1);
  1337. kaddr = kmap_atomic(page, KM_USER0);
  1338. left = __copy_from_user_inatomic(kaddr + offset,
  1339. buf, bytes);
  1340. kunmap_atomic(kaddr, KM_USER0);
  1341. }
  1342. if (left) {
  1343. kaddr = kmap(page);
  1344. left = __copy_from_user(kaddr + offset, buf, bytes);
  1345. kunmap(page);
  1346. }
  1347. written += bytes;
  1348. count -= bytes;
  1349. pos += bytes;
  1350. buf += bytes;
  1351. if (pos > inode->i_size)
  1352. i_size_write(inode, pos);
  1353. flush_dcache_page(page);
  1354. set_page_dirty(page);
  1355. mark_page_accessed(page);
  1356. page_cache_release(page);
  1357. if (left) {
  1358. pos -= left;
  1359. written -= left;
  1360. err = -EFAULT;
  1361. break;
  1362. }
  1363. /*
  1364. * Our dirty pages are not counted in nr_dirty,
  1365. * and we do not attempt to balance dirty pages.
  1366. */
  1367. cond_resched();
  1368. } while (count);
  1369. *ppos = pos;
  1370. if (written)
  1371. err = written;
  1372. out:
  1373. mutex_unlock(&inode->i_mutex);
  1374. return err;
  1375. }
  1376. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1377. {
  1378. struct inode *inode = filp->f_dentry->d_inode;
  1379. struct address_space *mapping = inode->i_mapping;
  1380. unsigned long index, offset;
  1381. index = *ppos >> PAGE_CACHE_SHIFT;
  1382. offset = *ppos & ~PAGE_CACHE_MASK;
  1383. for (;;) {
  1384. struct page *page = NULL;
  1385. unsigned long end_index, nr, ret;
  1386. loff_t i_size = i_size_read(inode);
  1387. end_index = i_size >> PAGE_CACHE_SHIFT;
  1388. if (index > end_index)
  1389. break;
  1390. if (index == end_index) {
  1391. nr = i_size & ~PAGE_CACHE_MASK;
  1392. if (nr <= offset)
  1393. break;
  1394. }
  1395. desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
  1396. if (desc->error) {
  1397. if (desc->error == -EINVAL)
  1398. desc->error = 0;
  1399. break;
  1400. }
  1401. /*
  1402. * We must evaluate after, since reads (unlike writes)
  1403. * are called without i_mutex protection against truncate
  1404. */
  1405. nr = PAGE_CACHE_SIZE;
  1406. i_size = i_size_read(inode);
  1407. end_index = i_size >> PAGE_CACHE_SHIFT;
  1408. if (index == end_index) {
  1409. nr = i_size & ~PAGE_CACHE_MASK;
  1410. if (nr <= offset) {
  1411. if (page)
  1412. page_cache_release(page);
  1413. break;
  1414. }
  1415. }
  1416. nr -= offset;
  1417. if (page) {
  1418. /*
  1419. * If users can be writing to this page using arbitrary
  1420. * virtual addresses, take care about potential aliasing
  1421. * before reading the page on the kernel side.
  1422. */
  1423. if (mapping_writably_mapped(mapping))
  1424. flush_dcache_page(page);
  1425. /*
  1426. * Mark the page accessed if we read the beginning.
  1427. */
  1428. if (!offset)
  1429. mark_page_accessed(page);
  1430. } else {
  1431. page = ZERO_PAGE(0);
  1432. page_cache_get(page);
  1433. }
  1434. /*
  1435. * Ok, we have the page, and it's up-to-date, so
  1436. * now we can copy it to user space...
  1437. *
  1438. * The actor routine returns how many bytes were actually used..
  1439. * NOTE! This may not be the same as how much of a user buffer
  1440. * we filled up (we may be padding etc), so we can only update
  1441. * "pos" here (the actor routine has to update the user buffer
  1442. * pointers and the remaining count).
  1443. */
  1444. ret = actor(desc, page, offset, nr);
  1445. offset += ret;
  1446. index += offset >> PAGE_CACHE_SHIFT;
  1447. offset &= ~PAGE_CACHE_MASK;
  1448. page_cache_release(page);
  1449. if (ret != nr || !desc->count)
  1450. break;
  1451. cond_resched();
  1452. }
  1453. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1454. file_accessed(filp);
  1455. }
  1456. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1457. {
  1458. read_descriptor_t desc;
  1459. if ((ssize_t) count < 0)
  1460. return -EINVAL;
  1461. if (!access_ok(VERIFY_WRITE, buf, count))
  1462. return -EFAULT;
  1463. if (!count)
  1464. return 0;
  1465. desc.written = 0;
  1466. desc.count = count;
  1467. desc.arg.buf = buf;
  1468. desc.error = 0;
  1469. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1470. if (desc.written)
  1471. return desc.written;
  1472. return desc.error;
  1473. }
  1474. static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
  1475. size_t count, read_actor_t actor, void *target)
  1476. {
  1477. read_descriptor_t desc;
  1478. if (!count)
  1479. return 0;
  1480. desc.written = 0;
  1481. desc.count = count;
  1482. desc.arg.data = target;
  1483. desc.error = 0;
  1484. do_shmem_file_read(in_file, ppos, &desc, actor);
  1485. if (desc.written)
  1486. return desc.written;
  1487. return desc.error;
  1488. }
  1489. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1490. {
  1491. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1492. buf->f_type = TMPFS_MAGIC;
  1493. buf->f_bsize = PAGE_CACHE_SIZE;
  1494. buf->f_namelen = NAME_MAX;
  1495. spin_lock(&sbinfo->stat_lock);
  1496. if (sbinfo->max_blocks) {
  1497. buf->f_blocks = sbinfo->max_blocks;
  1498. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1499. }
  1500. if (sbinfo->max_inodes) {
  1501. buf->f_files = sbinfo->max_inodes;
  1502. buf->f_ffree = sbinfo->free_inodes;
  1503. }
  1504. /* else leave those fields 0 like simple_statfs */
  1505. spin_unlock(&sbinfo->stat_lock);
  1506. return 0;
  1507. }
  1508. /*
  1509. * File creation. Allocate an inode, and we're done..
  1510. */
  1511. static int
  1512. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1513. {
  1514. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1515. int error = -ENOSPC;
  1516. if (inode) {
  1517. error = security_inode_init_security(inode, dir, NULL, NULL,
  1518. NULL);
  1519. if (error) {
  1520. if (error != -EOPNOTSUPP) {
  1521. iput(inode);
  1522. return error;
  1523. }
  1524. error = 0;
  1525. }
  1526. if (dir->i_mode & S_ISGID) {
  1527. inode->i_gid = dir->i_gid;
  1528. if (S_ISDIR(mode))
  1529. inode->i_mode |= S_ISGID;
  1530. }
  1531. dir->i_size += BOGO_DIRENT_SIZE;
  1532. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1533. d_instantiate(dentry, inode);
  1534. dget(dentry); /* Extra count - pin the dentry in core */
  1535. }
  1536. return error;
  1537. }
  1538. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1539. {
  1540. int error;
  1541. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1542. return error;
  1543. dir->i_nlink++;
  1544. return 0;
  1545. }
  1546. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1547. struct nameidata *nd)
  1548. {
  1549. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1550. }
  1551. /*
  1552. * Link a file..
  1553. */
  1554. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1555. {
  1556. struct inode *inode = old_dentry->d_inode;
  1557. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1558. /*
  1559. * No ordinary (disk based) filesystem counts links as inodes;
  1560. * but each new link needs a new dentry, pinning lowmem, and
  1561. * tmpfs dentries cannot be pruned until they are unlinked.
  1562. */
  1563. if (sbinfo->max_inodes) {
  1564. spin_lock(&sbinfo->stat_lock);
  1565. if (!sbinfo->free_inodes) {
  1566. spin_unlock(&sbinfo->stat_lock);
  1567. return -ENOSPC;
  1568. }
  1569. sbinfo->free_inodes--;
  1570. spin_unlock(&sbinfo->stat_lock);
  1571. }
  1572. dir->i_size += BOGO_DIRENT_SIZE;
  1573. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1574. inode->i_nlink++;
  1575. atomic_inc(&inode->i_count); /* New dentry reference */
  1576. dget(dentry); /* Extra pinning count for the created dentry */
  1577. d_instantiate(dentry, inode);
  1578. return 0;
  1579. }
  1580. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1581. {
  1582. struct inode *inode = dentry->d_inode;
  1583. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
  1584. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1585. if (sbinfo->max_inodes) {
  1586. spin_lock(&sbinfo->stat_lock);
  1587. sbinfo->free_inodes++;
  1588. spin_unlock(&sbinfo->stat_lock);
  1589. }
  1590. }
  1591. dir->i_size -= BOGO_DIRENT_SIZE;
  1592. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1593. inode->i_nlink--;
  1594. dput(dentry); /* Undo the count from "create" - this does all the work */
  1595. return 0;
  1596. }
  1597. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1598. {
  1599. if (!simple_empty(dentry))
  1600. return -ENOTEMPTY;
  1601. dentry->d_inode->i_nlink--;
  1602. dir->i_nlink--;
  1603. return shmem_unlink(dir, dentry);
  1604. }
  1605. /*
  1606. * The VFS layer already does all the dentry stuff for rename,
  1607. * we just have to decrement the usage count for the target if
  1608. * it exists so that the VFS layer correctly free's it when it
  1609. * gets overwritten.
  1610. */
  1611. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1612. {
  1613. struct inode *inode = old_dentry->d_inode;
  1614. int they_are_dirs = S_ISDIR(inode->i_mode);
  1615. if (!simple_empty(new_dentry))
  1616. return -ENOTEMPTY;
  1617. if (new_dentry->d_inode) {
  1618. (void) shmem_unlink(new_dir, new_dentry);
  1619. if (they_are_dirs)
  1620. old_dir->i_nlink--;
  1621. } else if (they_are_dirs) {
  1622. old_dir->i_nlink--;
  1623. new_dir->i_nlink++;
  1624. }
  1625. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1626. new_dir->i_size += BOGO_DIRENT_SIZE;
  1627. old_dir->i_ctime = old_dir->i_mtime =
  1628. new_dir->i_ctime = new_dir->i_mtime =
  1629. inode->i_ctime = CURRENT_TIME;
  1630. return 0;
  1631. }
  1632. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1633. {
  1634. int error;
  1635. int len;
  1636. struct inode *inode;
  1637. struct page *page = NULL;
  1638. char *kaddr;
  1639. struct shmem_inode_info *info;
  1640. len = strlen(symname) + 1;
  1641. if (len > PAGE_CACHE_SIZE)
  1642. return -ENAMETOOLONG;
  1643. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1644. if (!inode)
  1645. return -ENOSPC;
  1646. error = security_inode_init_security(inode, dir, NULL, NULL,
  1647. NULL);
  1648. if (error) {
  1649. if (error != -EOPNOTSUPP) {
  1650. iput(inode);
  1651. return error;
  1652. }
  1653. error = 0;
  1654. }
  1655. info = SHMEM_I(inode);
  1656. inode->i_size = len-1;
  1657. if (len <= (char *)inode - (char *)info) {
  1658. /* do it inline */
  1659. memcpy(info, symname, len);
  1660. inode->i_op = &shmem_symlink_inline_operations;
  1661. } else {
  1662. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1663. if (error) {
  1664. iput(inode);
  1665. return error;
  1666. }
  1667. inode->i_op = &shmem_symlink_inode_operations;
  1668. kaddr = kmap_atomic(page, KM_USER0);
  1669. memcpy(kaddr, symname, len);
  1670. kunmap_atomic(kaddr, KM_USER0);
  1671. set_page_dirty(page);
  1672. page_cache_release(page);
  1673. }
  1674. if (dir->i_mode & S_ISGID)
  1675. inode->i_gid = dir->i_gid;
  1676. dir->i_size += BOGO_DIRENT_SIZE;
  1677. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1678. d_instantiate(dentry, inode);
  1679. dget(dentry);
  1680. return 0;
  1681. }
  1682. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1683. {
  1684. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1685. return NULL;
  1686. }
  1687. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1688. {
  1689. struct page *page = NULL;
  1690. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1691. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1692. return page;
  1693. }
  1694. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1695. {
  1696. if (!IS_ERR(nd_get_link(nd))) {
  1697. struct page *page = cookie;
  1698. kunmap(page);
  1699. mark_page_accessed(page);
  1700. page_cache_release(page);
  1701. }
  1702. }
  1703. static struct inode_operations shmem_symlink_inline_operations = {
  1704. .readlink = generic_readlink,
  1705. .follow_link = shmem_follow_link_inline,
  1706. };
  1707. static struct inode_operations shmem_symlink_inode_operations = {
  1708. .truncate = shmem_truncate,
  1709. .readlink = generic_readlink,
  1710. .follow_link = shmem_follow_link,
  1711. .put_link = shmem_put_link,
  1712. };
  1713. static int shmem_parse_options(char *options, int *mode, uid_t *uid,
  1714. gid_t *gid, unsigned long *blocks, unsigned long *inodes,
  1715. int *policy, nodemask_t *policy_nodes)
  1716. {
  1717. char *this_char, *value, *rest;
  1718. while (options != NULL) {
  1719. this_char = options;
  1720. for (;;) {
  1721. /*
  1722. * NUL-terminate this option: unfortunately,
  1723. * mount options form a comma-separated list,
  1724. * but mpol's nodelist may also contain commas.
  1725. */
  1726. options = strchr(options, ',');
  1727. if (options == NULL)
  1728. break;
  1729. options++;
  1730. if (!isdigit(*options)) {
  1731. options[-1] = '\0';
  1732. break;
  1733. }
  1734. }
  1735. if (!*this_char)
  1736. continue;
  1737. if ((value = strchr(this_char,'=')) != NULL) {
  1738. *value++ = 0;
  1739. } else {
  1740. printk(KERN_ERR
  1741. "tmpfs: No value for mount option '%s'\n",
  1742. this_char);
  1743. return 1;
  1744. }
  1745. if (!strcmp(this_char,"size")) {
  1746. unsigned long long size;
  1747. size = memparse(value,&rest);
  1748. if (*rest == '%') {
  1749. size <<= PAGE_SHIFT;
  1750. size *= totalram_pages;
  1751. do_div(size, 100);
  1752. rest++;
  1753. }
  1754. if (*rest)
  1755. goto bad_val;
  1756. *blocks = size >> PAGE_CACHE_SHIFT;
  1757. } else if (!strcmp(this_char,"nr_blocks")) {
  1758. *blocks = memparse(value,&rest);
  1759. if (*rest)
  1760. goto bad_val;
  1761. } else if (!strcmp(this_char,"nr_inodes")) {
  1762. *inodes = memparse(value,&rest);
  1763. if (*rest)
  1764. goto bad_val;
  1765. } else if (!strcmp(this_char,"mode")) {
  1766. if (!mode)
  1767. continue;
  1768. *mode = simple_strtoul(value,&rest,8);
  1769. if (*rest)
  1770. goto bad_val;
  1771. } else if (!strcmp(this_char,"uid")) {
  1772. if (!uid)
  1773. continue;
  1774. *uid = simple_strtoul(value,&rest,0);
  1775. if (*rest)
  1776. goto bad_val;
  1777. } else if (!strcmp(this_char,"gid")) {
  1778. if (!gid)
  1779. continue;
  1780. *gid = simple_strtoul(value,&rest,0);
  1781. if (*rest)
  1782. goto bad_val;
  1783. } else if (!strcmp(this_char,"mpol")) {
  1784. if (shmem_parse_mpol(value,policy,policy_nodes))
  1785. goto bad_val;
  1786. } else {
  1787. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1788. this_char);
  1789. return 1;
  1790. }
  1791. }
  1792. return 0;
  1793. bad_val:
  1794. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1795. value, this_char);
  1796. return 1;
  1797. }
  1798. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1799. {
  1800. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1801. unsigned long max_blocks = sbinfo->max_blocks;
  1802. unsigned long max_inodes = sbinfo->max_inodes;
  1803. int policy = sbinfo->policy;
  1804. nodemask_t policy_nodes = sbinfo->policy_nodes;
  1805. unsigned long blocks;
  1806. unsigned long inodes;
  1807. int error = -EINVAL;
  1808. if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
  1809. &max_inodes, &policy, &policy_nodes))
  1810. return error;
  1811. spin_lock(&sbinfo->stat_lock);
  1812. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  1813. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1814. if (max_blocks < blocks)
  1815. goto out;
  1816. if (max_inodes < inodes)
  1817. goto out;
  1818. /*
  1819. * Those tests also disallow limited->unlimited while any are in
  1820. * use, so i_blocks will always be zero when max_blocks is zero;
  1821. * but we must separately disallow unlimited->limited, because
  1822. * in that case we have no record of how much is already in use.
  1823. */
  1824. if (max_blocks && !sbinfo->max_blocks)
  1825. goto out;
  1826. if (max_inodes && !sbinfo->max_inodes)
  1827. goto out;
  1828. error = 0;
  1829. sbinfo->max_blocks = max_blocks;
  1830. sbinfo->free_blocks = max_blocks - blocks;
  1831. sbinfo->max_inodes = max_inodes;
  1832. sbinfo->free_inodes = max_inodes - inodes;
  1833. sbinfo->policy = policy;
  1834. sbinfo->policy_nodes = policy_nodes;
  1835. out:
  1836. spin_unlock(&sbinfo->stat_lock);
  1837. return error;
  1838. }
  1839. #endif
  1840. static void shmem_put_super(struct super_block *sb)
  1841. {
  1842. kfree(sb->s_fs_info);
  1843. sb->s_fs_info = NULL;
  1844. }
  1845. static int shmem_fill_super(struct super_block *sb,
  1846. void *data, int silent)
  1847. {
  1848. struct inode *inode;
  1849. struct dentry *root;
  1850. int mode = S_IRWXUGO | S_ISVTX;
  1851. uid_t uid = current->fsuid;
  1852. gid_t gid = current->fsgid;
  1853. int err = -ENOMEM;
  1854. struct shmem_sb_info *sbinfo;
  1855. unsigned long blocks = 0;
  1856. unsigned long inodes = 0;
  1857. int policy = MPOL_DEFAULT;
  1858. nodemask_t policy_nodes = node_online_map;
  1859. #ifdef CONFIG_TMPFS
  1860. /*
  1861. * Per default we only allow half of the physical ram per
  1862. * tmpfs instance, limiting inodes to one per page of lowmem;
  1863. * but the internal instance is left unlimited.
  1864. */
  1865. if (!(sb->s_flags & MS_NOUSER)) {
  1866. blocks = totalram_pages / 2;
  1867. inodes = totalram_pages - totalhigh_pages;
  1868. if (inodes > blocks)
  1869. inodes = blocks;
  1870. if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
  1871. &inodes, &policy, &policy_nodes))
  1872. return -EINVAL;
  1873. }
  1874. #else
  1875. sb->s_flags |= MS_NOUSER;
  1876. #endif
  1877. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1878. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  1879. L1_CACHE_BYTES), GFP_KERNEL);
  1880. if (!sbinfo)
  1881. return -ENOMEM;
  1882. spin_lock_init(&sbinfo->stat_lock);
  1883. sbinfo->max_blocks = blocks;
  1884. sbinfo->free_blocks = blocks;
  1885. sbinfo->max_inodes = inodes;
  1886. sbinfo->free_inodes = inodes;
  1887. sbinfo->policy = policy;
  1888. sbinfo->policy_nodes = policy_nodes;
  1889. sb->s_fs_info = sbinfo;
  1890. sb->s_maxbytes = SHMEM_MAX_BYTES;
  1891. sb->s_blocksize = PAGE_CACHE_SIZE;
  1892. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1893. sb->s_magic = TMPFS_MAGIC;
  1894. sb->s_op = &shmem_ops;
  1895. sb->s_time_gran = 1;
  1896. inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
  1897. if (!inode)
  1898. goto failed;
  1899. inode->i_uid = uid;
  1900. inode->i_gid = gid;
  1901. root = d_alloc_root(inode);
  1902. if (!root)
  1903. goto failed_iput;
  1904. sb->s_root = root;
  1905. return 0;
  1906. failed_iput:
  1907. iput(inode);
  1908. failed:
  1909. shmem_put_super(sb);
  1910. return err;
  1911. }
  1912. static struct kmem_cache *shmem_inode_cachep;
  1913. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1914. {
  1915. struct shmem_inode_info *p;
  1916. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
  1917. if (!p)
  1918. return NULL;
  1919. return &p->vfs_inode;
  1920. }
  1921. static void shmem_destroy_inode(struct inode *inode)
  1922. {
  1923. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  1924. /* only struct inode is valid if it's an inline symlink */
  1925. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1926. }
  1927. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1928. }
  1929. static void init_once(void *foo, struct kmem_cache *cachep,
  1930. unsigned long flags)
  1931. {
  1932. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  1933. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1934. SLAB_CTOR_CONSTRUCTOR) {
  1935. inode_init_once(&p->vfs_inode);
  1936. }
  1937. }
  1938. static int init_inodecache(void)
  1939. {
  1940. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1941. sizeof(struct shmem_inode_info),
  1942. 0, 0, init_once, NULL);
  1943. if (shmem_inode_cachep == NULL)
  1944. return -ENOMEM;
  1945. return 0;
  1946. }
  1947. static void destroy_inodecache(void)
  1948. {
  1949. if (kmem_cache_destroy(shmem_inode_cachep))
  1950. printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
  1951. }
  1952. static const struct address_space_operations shmem_aops = {
  1953. .writepage = shmem_writepage,
  1954. .set_page_dirty = __set_page_dirty_nobuffers,
  1955. #ifdef CONFIG_TMPFS
  1956. .prepare_write = shmem_prepare_write,
  1957. .commit_write = simple_commit_write,
  1958. #endif
  1959. .migratepage = migrate_page,
  1960. };
  1961. static struct file_operations shmem_file_operations = {
  1962. .mmap = shmem_mmap,
  1963. #ifdef CONFIG_TMPFS
  1964. .llseek = generic_file_llseek,
  1965. .read = shmem_file_read,
  1966. .write = shmem_file_write,
  1967. .fsync = simple_sync_file,
  1968. .sendfile = shmem_file_sendfile,
  1969. #endif
  1970. };
  1971. static struct inode_operations shmem_inode_operations = {
  1972. .truncate = shmem_truncate,
  1973. .setattr = shmem_notify_change,
  1974. .truncate_range = shmem_truncate_range,
  1975. };
  1976. static struct inode_operations shmem_dir_inode_operations = {
  1977. #ifdef CONFIG_TMPFS
  1978. .create = shmem_create,
  1979. .lookup = simple_lookup,
  1980. .link = shmem_link,
  1981. .unlink = shmem_unlink,
  1982. .symlink = shmem_symlink,
  1983. .mkdir = shmem_mkdir,
  1984. .rmdir = shmem_rmdir,
  1985. .mknod = shmem_mknod,
  1986. .rename = shmem_rename,
  1987. #endif
  1988. };
  1989. static struct super_operations shmem_ops = {
  1990. .alloc_inode = shmem_alloc_inode,
  1991. .destroy_inode = shmem_destroy_inode,
  1992. #ifdef CONFIG_TMPFS
  1993. .statfs = shmem_statfs,
  1994. .remount_fs = shmem_remount_fs,
  1995. #endif
  1996. .delete_inode = shmem_delete_inode,
  1997. .drop_inode = generic_delete_inode,
  1998. .put_super = shmem_put_super,
  1999. };
  2000. static struct vm_operations_struct shmem_vm_ops = {
  2001. .nopage = shmem_nopage,
  2002. .populate = shmem_populate,
  2003. #ifdef CONFIG_NUMA
  2004. .set_policy = shmem_set_policy,
  2005. .get_policy = shmem_get_policy,
  2006. #endif
  2007. };
  2008. static int shmem_get_sb(struct file_system_type *fs_type,
  2009. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  2010. {
  2011. return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
  2012. }
  2013. static struct file_system_type tmpfs_fs_type = {
  2014. .owner = THIS_MODULE,
  2015. .name = "tmpfs",
  2016. .get_sb = shmem_get_sb,
  2017. .kill_sb = kill_litter_super,
  2018. };
  2019. static struct vfsmount *shm_mnt;
  2020. static int __init init_tmpfs(void)
  2021. {
  2022. int error;
  2023. error = init_inodecache();
  2024. if (error)
  2025. goto out3;
  2026. error = register_filesystem(&tmpfs_fs_type);
  2027. if (error) {
  2028. printk(KERN_ERR "Could not register tmpfs\n");
  2029. goto out2;
  2030. }
  2031. shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
  2032. tmpfs_fs_type.name, NULL);
  2033. if (IS_ERR(shm_mnt)) {
  2034. error = PTR_ERR(shm_mnt);
  2035. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2036. goto out1;
  2037. }
  2038. return 0;
  2039. out1:
  2040. unregister_filesystem(&tmpfs_fs_type);
  2041. out2:
  2042. destroy_inodecache();
  2043. out3:
  2044. shm_mnt = ERR_PTR(error);
  2045. return error;
  2046. }
  2047. module_init(init_tmpfs)
  2048. /*
  2049. * shmem_file_setup - get an unlinked file living in tmpfs
  2050. *
  2051. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2052. * @size: size to be set for the file
  2053. *
  2054. */
  2055. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2056. {
  2057. int error;
  2058. struct file *file;
  2059. struct inode *inode;
  2060. struct dentry *dentry, *root;
  2061. struct qstr this;
  2062. if (IS_ERR(shm_mnt))
  2063. return (void *)shm_mnt;
  2064. if (size < 0 || size > SHMEM_MAX_BYTES)
  2065. return ERR_PTR(-EINVAL);
  2066. if (shmem_acct_size(flags, size))
  2067. return ERR_PTR(-ENOMEM);
  2068. error = -ENOMEM;
  2069. this.name = name;
  2070. this.len = strlen(name);
  2071. this.hash = 0; /* will go */
  2072. root = shm_mnt->mnt_root;
  2073. dentry = d_alloc(root, &this);
  2074. if (!dentry)
  2075. goto put_memory;
  2076. error = -ENFILE;
  2077. file = get_empty_filp();
  2078. if (!file)
  2079. goto put_dentry;
  2080. error = -ENOSPC;
  2081. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2082. if (!inode)
  2083. goto close_file;
  2084. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2085. d_instantiate(dentry, inode);
  2086. inode->i_size = size;
  2087. inode->i_nlink = 0; /* It is unlinked */
  2088. file->f_vfsmnt = mntget(shm_mnt);
  2089. file->f_dentry = dentry;
  2090. file->f_mapping = inode->i_mapping;
  2091. file->f_op = &shmem_file_operations;
  2092. file->f_mode = FMODE_WRITE | FMODE_READ;
  2093. return file;
  2094. close_file:
  2095. put_filp(file);
  2096. put_dentry:
  2097. dput(dentry);
  2098. put_memory:
  2099. shmem_unacct_size(flags, size);
  2100. return ERR_PTR(error);
  2101. }
  2102. /*
  2103. * shmem_zero_setup - setup a shared anonymous mapping
  2104. *
  2105. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2106. */
  2107. int shmem_zero_setup(struct vm_area_struct *vma)
  2108. {
  2109. struct file *file;
  2110. loff_t size = vma->vm_end - vma->vm_start;
  2111. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2112. if (IS_ERR(file))
  2113. return PTR_ERR(file);
  2114. if (vma->vm_file)
  2115. fput(vma->vm_file);
  2116. vma->vm_file = file;
  2117. vma->vm_ops = &shmem_vm_ops;
  2118. return 0;
  2119. }