shmem.c 58 KB

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