shmem.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  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 <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 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 struct page *shmem_swapin_async(struct shared_policy *p,
  800. swp_entry_t entry, unsigned long idx)
  801. {
  802. struct page *page;
  803. struct vm_area_struct pvma;
  804. /* Create a pseudo vma that just contains the policy */
  805. memset(&pvma, 0, sizeof(struct vm_area_struct));
  806. pvma.vm_end = PAGE_SIZE;
  807. pvma.vm_pgoff = idx;
  808. pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
  809. page = read_swap_cache_async(entry, &pvma, 0);
  810. mpol_free(pvma.vm_policy);
  811. return page;
  812. }
  813. struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
  814. unsigned long idx)
  815. {
  816. struct shared_policy *p = &info->policy;
  817. int i, num;
  818. struct page *page;
  819. unsigned long offset;
  820. num = valid_swaphandles(entry, &offset);
  821. for (i = 0; i < num; offset++, i++) {
  822. page = shmem_swapin_async(p,
  823. swp_entry(swp_type(entry), offset), idx);
  824. if (!page)
  825. break;
  826. page_cache_release(page);
  827. }
  828. lru_add_drain(); /* Push any new pages onto the LRU now */
  829. return shmem_swapin_async(p, entry, idx);
  830. }
  831. static struct page *
  832. shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
  833. unsigned long idx)
  834. {
  835. struct vm_area_struct pvma;
  836. struct page *page;
  837. memset(&pvma, 0, sizeof(struct vm_area_struct));
  838. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
  839. pvma.vm_pgoff = idx;
  840. pvma.vm_end = PAGE_SIZE;
  841. page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
  842. mpol_free(pvma.vm_policy);
  843. return page;
  844. }
  845. #else
  846. static inline struct page *
  847. shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
  848. {
  849. swapin_readahead(entry, 0, NULL);
  850. return read_swap_cache_async(entry, NULL, 0);
  851. }
  852. static inline struct page *
  853. shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
  854. {
  855. return alloc_page(gfp | __GFP_ZERO);
  856. }
  857. #endif
  858. /*
  859. * shmem_getpage - either get the page from swap or allocate a new one
  860. *
  861. * If we allocate a new one we do not mark it dirty. That's up to the
  862. * vm. If we swap it in we mark it dirty since we also free the swap
  863. * entry since a page cannot live in both the swap and page cache
  864. */
  865. static int shmem_getpage(struct inode *inode, unsigned long idx,
  866. struct page **pagep, enum sgp_type sgp, int *type)
  867. {
  868. struct address_space *mapping = inode->i_mapping;
  869. struct shmem_inode_info *info = SHMEM_I(inode);
  870. struct shmem_sb_info *sbinfo;
  871. struct page *filepage = *pagep;
  872. struct page *swappage;
  873. swp_entry_t *entry;
  874. swp_entry_t swap;
  875. int error;
  876. if (idx >= SHMEM_MAX_INDEX)
  877. return -EFBIG;
  878. /*
  879. * Normally, filepage is NULL on entry, and either found
  880. * uptodate immediately, or allocated and zeroed, or read
  881. * in under swappage, which is then assigned to filepage.
  882. * But shmem_prepare_write passes in a locked filepage,
  883. * which may be found not uptodate by other callers too,
  884. * and may need to be copied from the swappage read in.
  885. */
  886. repeat:
  887. if (!filepage)
  888. filepage = find_lock_page(mapping, idx);
  889. if (filepage && PageUptodate(filepage))
  890. goto done;
  891. error = 0;
  892. if (sgp == SGP_QUICK)
  893. goto failed;
  894. spin_lock(&info->lock);
  895. shmem_recalc_inode(inode);
  896. entry = shmem_swp_alloc(info, idx, sgp);
  897. if (IS_ERR(entry)) {
  898. spin_unlock(&info->lock);
  899. error = PTR_ERR(entry);
  900. goto failed;
  901. }
  902. swap = *entry;
  903. if (swap.val) {
  904. /* Look it up and read it in.. */
  905. swappage = lookup_swap_cache(swap);
  906. if (!swappage) {
  907. shmem_swp_unmap(entry);
  908. spin_unlock(&info->lock);
  909. /* here we actually do the io */
  910. if (type && *type == VM_FAULT_MINOR) {
  911. inc_page_state(pgmajfault);
  912. *type = VM_FAULT_MAJOR;
  913. }
  914. swappage = shmem_swapin(info, swap, idx);
  915. if (!swappage) {
  916. spin_lock(&info->lock);
  917. entry = shmem_swp_alloc(info, idx, sgp);
  918. if (IS_ERR(entry))
  919. error = PTR_ERR(entry);
  920. else {
  921. if (entry->val == swap.val)
  922. error = -ENOMEM;
  923. shmem_swp_unmap(entry);
  924. }
  925. spin_unlock(&info->lock);
  926. if (error)
  927. goto failed;
  928. goto repeat;
  929. }
  930. wait_on_page_locked(swappage);
  931. page_cache_release(swappage);
  932. goto repeat;
  933. }
  934. /* We have to do this with page locked to prevent races */
  935. if (TestSetPageLocked(swappage)) {
  936. shmem_swp_unmap(entry);
  937. spin_unlock(&info->lock);
  938. wait_on_page_locked(swappage);
  939. page_cache_release(swappage);
  940. goto repeat;
  941. }
  942. if (PageWriteback(swappage)) {
  943. shmem_swp_unmap(entry);
  944. spin_unlock(&info->lock);
  945. wait_on_page_writeback(swappage);
  946. unlock_page(swappage);
  947. page_cache_release(swappage);
  948. goto repeat;
  949. }
  950. if (!PageUptodate(swappage)) {
  951. shmem_swp_unmap(entry);
  952. spin_unlock(&info->lock);
  953. unlock_page(swappage);
  954. page_cache_release(swappage);
  955. error = -EIO;
  956. goto failed;
  957. }
  958. if (filepage) {
  959. shmem_swp_set(info, entry, 0);
  960. shmem_swp_unmap(entry);
  961. delete_from_swap_cache(swappage);
  962. spin_unlock(&info->lock);
  963. copy_highpage(filepage, swappage);
  964. unlock_page(swappage);
  965. page_cache_release(swappage);
  966. flush_dcache_page(filepage);
  967. SetPageUptodate(filepage);
  968. set_page_dirty(filepage);
  969. swap_free(swap);
  970. } else if (!(error = move_from_swap_cache(
  971. swappage, idx, mapping))) {
  972. info->flags |= SHMEM_PAGEIN;
  973. shmem_swp_set(info, entry, 0);
  974. shmem_swp_unmap(entry);
  975. spin_unlock(&info->lock);
  976. filepage = swappage;
  977. swap_free(swap);
  978. } else {
  979. shmem_swp_unmap(entry);
  980. spin_unlock(&info->lock);
  981. unlock_page(swappage);
  982. page_cache_release(swappage);
  983. if (error == -ENOMEM) {
  984. /* let kswapd refresh zone for GFP_ATOMICs */
  985. blk_congestion_wait(WRITE, HZ/50);
  986. }
  987. goto repeat;
  988. }
  989. } else if (sgp == SGP_READ && !filepage) {
  990. shmem_swp_unmap(entry);
  991. filepage = find_get_page(mapping, idx);
  992. if (filepage &&
  993. (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
  994. spin_unlock(&info->lock);
  995. wait_on_page_locked(filepage);
  996. page_cache_release(filepage);
  997. filepage = NULL;
  998. goto repeat;
  999. }
  1000. spin_unlock(&info->lock);
  1001. } else {
  1002. shmem_swp_unmap(entry);
  1003. sbinfo = SHMEM_SB(inode->i_sb);
  1004. if (sbinfo->max_blocks) {
  1005. spin_lock(&sbinfo->stat_lock);
  1006. if (sbinfo->free_blocks == 0 ||
  1007. shmem_acct_block(info->flags)) {
  1008. spin_unlock(&sbinfo->stat_lock);
  1009. spin_unlock(&info->lock);
  1010. error = -ENOSPC;
  1011. goto failed;
  1012. }
  1013. sbinfo->free_blocks--;
  1014. inode->i_blocks += BLOCKS_PER_PAGE;
  1015. spin_unlock(&sbinfo->stat_lock);
  1016. } else if (shmem_acct_block(info->flags)) {
  1017. spin_unlock(&info->lock);
  1018. error = -ENOSPC;
  1019. goto failed;
  1020. }
  1021. if (!filepage) {
  1022. spin_unlock(&info->lock);
  1023. filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
  1024. info,
  1025. idx);
  1026. if (!filepage) {
  1027. shmem_unacct_blocks(info->flags, 1);
  1028. shmem_free_blocks(inode, 1);
  1029. error = -ENOMEM;
  1030. goto failed;
  1031. }
  1032. spin_lock(&info->lock);
  1033. entry = shmem_swp_alloc(info, idx, sgp);
  1034. if (IS_ERR(entry))
  1035. error = PTR_ERR(entry);
  1036. else {
  1037. swap = *entry;
  1038. shmem_swp_unmap(entry);
  1039. }
  1040. if (error || swap.val || 0 != add_to_page_cache_lru(
  1041. filepage, mapping, idx, GFP_ATOMIC)) {
  1042. spin_unlock(&info->lock);
  1043. page_cache_release(filepage);
  1044. shmem_unacct_blocks(info->flags, 1);
  1045. shmem_free_blocks(inode, 1);
  1046. filepage = NULL;
  1047. if (error)
  1048. goto failed;
  1049. goto repeat;
  1050. }
  1051. info->flags |= SHMEM_PAGEIN;
  1052. }
  1053. info->alloced++;
  1054. spin_unlock(&info->lock);
  1055. flush_dcache_page(filepage);
  1056. SetPageUptodate(filepage);
  1057. }
  1058. done:
  1059. if (*pagep != filepage) {
  1060. unlock_page(filepage);
  1061. *pagep = filepage;
  1062. }
  1063. return 0;
  1064. failed:
  1065. if (*pagep != filepage) {
  1066. unlock_page(filepage);
  1067. page_cache_release(filepage);
  1068. }
  1069. return error;
  1070. }
  1071. struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
  1072. {
  1073. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1074. struct page *page = NULL;
  1075. unsigned long idx;
  1076. int error;
  1077. idx = (address - vma->vm_start) >> PAGE_SHIFT;
  1078. idx += vma->vm_pgoff;
  1079. idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
  1080. if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  1081. return NOPAGE_SIGBUS;
  1082. error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
  1083. if (error)
  1084. return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
  1085. mark_page_accessed(page);
  1086. return page;
  1087. }
  1088. static int shmem_populate(struct vm_area_struct *vma,
  1089. unsigned long addr, unsigned long len,
  1090. pgprot_t prot, unsigned long pgoff, int nonblock)
  1091. {
  1092. struct inode *inode = vma->vm_file->f_dentry->d_inode;
  1093. struct mm_struct *mm = vma->vm_mm;
  1094. enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
  1095. unsigned long size;
  1096. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1097. if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
  1098. return -EINVAL;
  1099. while ((long) len > 0) {
  1100. struct page *page = NULL;
  1101. int err;
  1102. /*
  1103. * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
  1104. */
  1105. err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
  1106. if (err)
  1107. return err;
  1108. /* Page may still be null, but only if nonblock was set. */
  1109. if (page) {
  1110. mark_page_accessed(page);
  1111. err = install_page(mm, vma, addr, page, prot);
  1112. if (err) {
  1113. page_cache_release(page);
  1114. return err;
  1115. }
  1116. } else if (vma->vm_flags & VM_NONLINEAR) {
  1117. /* No page was found just because we can't read it in
  1118. * now (being here implies nonblock != 0), but the page
  1119. * may exist, so set the PTE to fault it in later. */
  1120. err = install_file_pte(mm, vma, addr, pgoff, prot);
  1121. if (err)
  1122. return err;
  1123. }
  1124. len -= PAGE_SIZE;
  1125. addr += PAGE_SIZE;
  1126. pgoff++;
  1127. }
  1128. return 0;
  1129. }
  1130. #ifdef CONFIG_NUMA
  1131. int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  1132. {
  1133. struct inode *i = vma->vm_file->f_dentry->d_inode;
  1134. return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
  1135. }
  1136. struct mempolicy *
  1137. shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
  1138. {
  1139. struct inode *i = vma->vm_file->f_dentry->d_inode;
  1140. unsigned long idx;
  1141. idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1142. return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
  1143. }
  1144. #endif
  1145. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1146. {
  1147. struct inode *inode = file->f_dentry->d_inode;
  1148. struct shmem_inode_info *info = SHMEM_I(inode);
  1149. int retval = -ENOMEM;
  1150. spin_lock(&info->lock);
  1151. if (lock && !(info->flags & VM_LOCKED)) {
  1152. if (!user_shm_lock(inode->i_size, user))
  1153. goto out_nomem;
  1154. info->flags |= VM_LOCKED;
  1155. }
  1156. if (!lock && (info->flags & VM_LOCKED) && user) {
  1157. user_shm_unlock(inode->i_size, user);
  1158. info->flags &= ~VM_LOCKED;
  1159. }
  1160. retval = 0;
  1161. out_nomem:
  1162. spin_unlock(&info->lock);
  1163. return retval;
  1164. }
  1165. int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1166. {
  1167. file_accessed(file);
  1168. vma->vm_ops = &shmem_vm_ops;
  1169. return 0;
  1170. }
  1171. static struct inode *
  1172. shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
  1173. {
  1174. struct inode *inode;
  1175. struct shmem_inode_info *info;
  1176. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1177. if (sbinfo->max_inodes) {
  1178. spin_lock(&sbinfo->stat_lock);
  1179. if (!sbinfo->free_inodes) {
  1180. spin_unlock(&sbinfo->stat_lock);
  1181. return NULL;
  1182. }
  1183. sbinfo->free_inodes--;
  1184. spin_unlock(&sbinfo->stat_lock);
  1185. }
  1186. inode = new_inode(sb);
  1187. if (inode) {
  1188. inode->i_mode = mode;
  1189. inode->i_uid = current->fsuid;
  1190. inode->i_gid = current->fsgid;
  1191. inode->i_blksize = PAGE_CACHE_SIZE;
  1192. inode->i_blocks = 0;
  1193. inode->i_mapping->a_ops = &shmem_aops;
  1194. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1195. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1196. info = SHMEM_I(inode);
  1197. memset(info, 0, (char *)inode - (char *)info);
  1198. spin_lock_init(&info->lock);
  1199. INIT_LIST_HEAD(&info->swaplist);
  1200. switch (mode & S_IFMT) {
  1201. default:
  1202. init_special_inode(inode, mode, dev);
  1203. break;
  1204. case S_IFREG:
  1205. inode->i_op = &shmem_inode_operations;
  1206. inode->i_fop = &shmem_file_operations;
  1207. mpol_shared_policy_init(&info->policy, sbinfo->policy,
  1208. &sbinfo->policy_nodes);
  1209. break;
  1210. case S_IFDIR:
  1211. inode->i_nlink++;
  1212. /* Some things misbehave if size == 0 on a directory */
  1213. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1214. inode->i_op = &shmem_dir_inode_operations;
  1215. inode->i_fop = &simple_dir_operations;
  1216. break;
  1217. case S_IFLNK:
  1218. /*
  1219. * Must not load anything in the rbtree,
  1220. * mpol_free_shared_policy will not be called.
  1221. */
  1222. mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
  1223. NULL);
  1224. break;
  1225. }
  1226. } else if (sbinfo->max_inodes) {
  1227. spin_lock(&sbinfo->stat_lock);
  1228. sbinfo->free_inodes++;
  1229. spin_unlock(&sbinfo->stat_lock);
  1230. }
  1231. return inode;
  1232. }
  1233. #ifdef CONFIG_TMPFS
  1234. static struct inode_operations shmem_symlink_inode_operations;
  1235. static struct inode_operations shmem_symlink_inline_operations;
  1236. /*
  1237. * Normally tmpfs makes no use of shmem_prepare_write, but it
  1238. * lets a tmpfs file be used read-write below the loop driver.
  1239. */
  1240. static int
  1241. shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
  1242. {
  1243. struct inode *inode = page->mapping->host;
  1244. return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
  1245. }
  1246. static ssize_t
  1247. shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  1248. {
  1249. struct inode *inode = file->f_dentry->d_inode;
  1250. loff_t pos;
  1251. unsigned long written;
  1252. ssize_t err;
  1253. if ((ssize_t) count < 0)
  1254. return -EINVAL;
  1255. if (!access_ok(VERIFY_READ, buf, count))
  1256. return -EFAULT;
  1257. mutex_lock(&inode->i_mutex);
  1258. pos = *ppos;
  1259. written = 0;
  1260. err = generic_write_checks(file, &pos, &count, 0);
  1261. if (err || !count)
  1262. goto out;
  1263. err = remove_suid(file->f_dentry);
  1264. if (err)
  1265. goto out;
  1266. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1267. do {
  1268. struct page *page = NULL;
  1269. unsigned long bytes, index, offset;
  1270. char *kaddr;
  1271. int left;
  1272. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  1273. index = pos >> PAGE_CACHE_SHIFT;
  1274. bytes = PAGE_CACHE_SIZE - offset;
  1275. if (bytes > count)
  1276. bytes = count;
  1277. /*
  1278. * We don't hold page lock across copy from user -
  1279. * what would it guard against? - so no deadlock here.
  1280. * But it still may be a good idea to prefault below.
  1281. */
  1282. err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
  1283. if (err)
  1284. break;
  1285. left = bytes;
  1286. if (PageHighMem(page)) {
  1287. volatile unsigned char dummy;
  1288. __get_user(dummy, buf);
  1289. __get_user(dummy, buf + bytes - 1);
  1290. kaddr = kmap_atomic(page, KM_USER0);
  1291. left = __copy_from_user_inatomic(kaddr + offset,
  1292. buf, bytes);
  1293. kunmap_atomic(kaddr, KM_USER0);
  1294. }
  1295. if (left) {
  1296. kaddr = kmap(page);
  1297. left = __copy_from_user(kaddr + offset, buf, bytes);
  1298. kunmap(page);
  1299. }
  1300. written += bytes;
  1301. count -= bytes;
  1302. pos += bytes;
  1303. buf += bytes;
  1304. if (pos > inode->i_size)
  1305. i_size_write(inode, pos);
  1306. flush_dcache_page(page);
  1307. set_page_dirty(page);
  1308. mark_page_accessed(page);
  1309. page_cache_release(page);
  1310. if (left) {
  1311. pos -= left;
  1312. written -= left;
  1313. err = -EFAULT;
  1314. break;
  1315. }
  1316. /*
  1317. * Our dirty pages are not counted in nr_dirty,
  1318. * and we do not attempt to balance dirty pages.
  1319. */
  1320. cond_resched();
  1321. } while (count);
  1322. *ppos = pos;
  1323. if (written)
  1324. err = written;
  1325. out:
  1326. mutex_unlock(&inode->i_mutex);
  1327. return err;
  1328. }
  1329. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1330. {
  1331. struct inode *inode = filp->f_dentry->d_inode;
  1332. struct address_space *mapping = inode->i_mapping;
  1333. unsigned long index, offset;
  1334. index = *ppos >> PAGE_CACHE_SHIFT;
  1335. offset = *ppos & ~PAGE_CACHE_MASK;
  1336. for (;;) {
  1337. struct page *page = NULL;
  1338. unsigned long end_index, nr, ret;
  1339. loff_t i_size = i_size_read(inode);
  1340. end_index = i_size >> PAGE_CACHE_SHIFT;
  1341. if (index > end_index)
  1342. break;
  1343. if (index == end_index) {
  1344. nr = i_size & ~PAGE_CACHE_MASK;
  1345. if (nr <= offset)
  1346. break;
  1347. }
  1348. desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
  1349. if (desc->error) {
  1350. if (desc->error == -EINVAL)
  1351. desc->error = 0;
  1352. break;
  1353. }
  1354. /*
  1355. * We must evaluate after, since reads (unlike writes)
  1356. * are called without i_mutex protection against truncate
  1357. */
  1358. nr = PAGE_CACHE_SIZE;
  1359. i_size = i_size_read(inode);
  1360. end_index = i_size >> PAGE_CACHE_SHIFT;
  1361. if (index == end_index) {
  1362. nr = i_size & ~PAGE_CACHE_MASK;
  1363. if (nr <= offset) {
  1364. if (page)
  1365. page_cache_release(page);
  1366. break;
  1367. }
  1368. }
  1369. nr -= offset;
  1370. if (page) {
  1371. /*
  1372. * If users can be writing to this page using arbitrary
  1373. * virtual addresses, take care about potential aliasing
  1374. * before reading the page on the kernel side.
  1375. */
  1376. if (mapping_writably_mapped(mapping))
  1377. flush_dcache_page(page);
  1378. /*
  1379. * Mark the page accessed if we read the beginning.
  1380. */
  1381. if (!offset)
  1382. mark_page_accessed(page);
  1383. } else {
  1384. page = ZERO_PAGE(0);
  1385. page_cache_get(page);
  1386. }
  1387. /*
  1388. * Ok, we have the page, and it's up-to-date, so
  1389. * now we can copy it to user space...
  1390. *
  1391. * The actor routine returns how many bytes were actually used..
  1392. * NOTE! This may not be the same as how much of a user buffer
  1393. * we filled up (we may be padding etc), so we can only update
  1394. * "pos" here (the actor routine has to update the user buffer
  1395. * pointers and the remaining count).
  1396. */
  1397. ret = actor(desc, page, offset, nr);
  1398. offset += ret;
  1399. index += offset >> PAGE_CACHE_SHIFT;
  1400. offset &= ~PAGE_CACHE_MASK;
  1401. page_cache_release(page);
  1402. if (ret != nr || !desc->count)
  1403. break;
  1404. cond_resched();
  1405. }
  1406. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1407. file_accessed(filp);
  1408. }
  1409. static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  1410. {
  1411. read_descriptor_t desc;
  1412. if ((ssize_t) count < 0)
  1413. return -EINVAL;
  1414. if (!access_ok(VERIFY_WRITE, buf, count))
  1415. return -EFAULT;
  1416. if (!count)
  1417. return 0;
  1418. desc.written = 0;
  1419. desc.count = count;
  1420. desc.arg.buf = buf;
  1421. desc.error = 0;
  1422. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1423. if (desc.written)
  1424. return desc.written;
  1425. return desc.error;
  1426. }
  1427. static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
  1428. size_t count, read_actor_t actor, void *target)
  1429. {
  1430. read_descriptor_t desc;
  1431. if (!count)
  1432. return 0;
  1433. desc.written = 0;
  1434. desc.count = count;
  1435. desc.arg.data = target;
  1436. desc.error = 0;
  1437. do_shmem_file_read(in_file, ppos, &desc, actor);
  1438. if (desc.written)
  1439. return desc.written;
  1440. return desc.error;
  1441. }
  1442. static int shmem_statfs(struct super_block *sb, struct kstatfs *buf)
  1443. {
  1444. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1445. buf->f_type = TMPFS_MAGIC;
  1446. buf->f_bsize = PAGE_CACHE_SIZE;
  1447. buf->f_namelen = NAME_MAX;
  1448. spin_lock(&sbinfo->stat_lock);
  1449. if (sbinfo->max_blocks) {
  1450. buf->f_blocks = sbinfo->max_blocks;
  1451. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  1452. }
  1453. if (sbinfo->max_inodes) {
  1454. buf->f_files = sbinfo->max_inodes;
  1455. buf->f_ffree = sbinfo->free_inodes;
  1456. }
  1457. /* else leave those fields 0 like simple_statfs */
  1458. spin_unlock(&sbinfo->stat_lock);
  1459. return 0;
  1460. }
  1461. /*
  1462. * File creation. Allocate an inode, and we're done..
  1463. */
  1464. static int
  1465. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1466. {
  1467. struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
  1468. int error = -ENOSPC;
  1469. if (inode) {
  1470. error = security_inode_init_security(inode, dir, NULL, NULL,
  1471. NULL);
  1472. if (error) {
  1473. if (error != -EOPNOTSUPP) {
  1474. iput(inode);
  1475. return error;
  1476. }
  1477. error = 0;
  1478. }
  1479. if (dir->i_mode & S_ISGID) {
  1480. inode->i_gid = dir->i_gid;
  1481. if (S_ISDIR(mode))
  1482. inode->i_mode |= S_ISGID;
  1483. }
  1484. dir->i_size += BOGO_DIRENT_SIZE;
  1485. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1486. d_instantiate(dentry, inode);
  1487. dget(dentry); /* Extra count - pin the dentry in core */
  1488. }
  1489. return error;
  1490. }
  1491. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1492. {
  1493. int error;
  1494. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1495. return error;
  1496. dir->i_nlink++;
  1497. return 0;
  1498. }
  1499. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1500. struct nameidata *nd)
  1501. {
  1502. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1503. }
  1504. /*
  1505. * Link a file..
  1506. */
  1507. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1508. {
  1509. struct inode *inode = old_dentry->d_inode;
  1510. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1511. /*
  1512. * No ordinary (disk based) filesystem counts links as inodes;
  1513. * but each new link needs a new dentry, pinning lowmem, and
  1514. * tmpfs dentries cannot be pruned until they are unlinked.
  1515. */
  1516. if (sbinfo->max_inodes) {
  1517. spin_lock(&sbinfo->stat_lock);
  1518. if (!sbinfo->free_inodes) {
  1519. spin_unlock(&sbinfo->stat_lock);
  1520. return -ENOSPC;
  1521. }
  1522. sbinfo->free_inodes--;
  1523. spin_unlock(&sbinfo->stat_lock);
  1524. }
  1525. dir->i_size += BOGO_DIRENT_SIZE;
  1526. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1527. inode->i_nlink++;
  1528. atomic_inc(&inode->i_count); /* New dentry reference */
  1529. dget(dentry); /* Extra pinning count for the created dentry */
  1530. d_instantiate(dentry, inode);
  1531. return 0;
  1532. }
  1533. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1534. {
  1535. struct inode *inode = dentry->d_inode;
  1536. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
  1537. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1538. if (sbinfo->max_inodes) {
  1539. spin_lock(&sbinfo->stat_lock);
  1540. sbinfo->free_inodes++;
  1541. spin_unlock(&sbinfo->stat_lock);
  1542. }
  1543. }
  1544. dir->i_size -= BOGO_DIRENT_SIZE;
  1545. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1546. inode->i_nlink--;
  1547. dput(dentry); /* Undo the count from "create" - this does all the work */
  1548. return 0;
  1549. }
  1550. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1551. {
  1552. if (!simple_empty(dentry))
  1553. return -ENOTEMPTY;
  1554. dir->i_nlink--;
  1555. return shmem_unlink(dir, dentry);
  1556. }
  1557. /*
  1558. * The VFS layer already does all the dentry stuff for rename,
  1559. * we just have to decrement the usage count for the target if
  1560. * it exists so that the VFS layer correctly free's it when it
  1561. * gets overwritten.
  1562. */
  1563. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1564. {
  1565. struct inode *inode = old_dentry->d_inode;
  1566. int they_are_dirs = S_ISDIR(inode->i_mode);
  1567. if (!simple_empty(new_dentry))
  1568. return -ENOTEMPTY;
  1569. if (new_dentry->d_inode) {
  1570. (void) shmem_unlink(new_dir, new_dentry);
  1571. if (they_are_dirs)
  1572. old_dir->i_nlink--;
  1573. } else if (they_are_dirs) {
  1574. old_dir->i_nlink--;
  1575. new_dir->i_nlink++;
  1576. }
  1577. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1578. new_dir->i_size += BOGO_DIRENT_SIZE;
  1579. old_dir->i_ctime = old_dir->i_mtime =
  1580. new_dir->i_ctime = new_dir->i_mtime =
  1581. inode->i_ctime = CURRENT_TIME;
  1582. return 0;
  1583. }
  1584. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1585. {
  1586. int error;
  1587. int len;
  1588. struct inode *inode;
  1589. struct page *page = NULL;
  1590. char *kaddr;
  1591. struct shmem_inode_info *info;
  1592. len = strlen(symname) + 1;
  1593. if (len > PAGE_CACHE_SIZE)
  1594. return -ENAMETOOLONG;
  1595. inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  1596. if (!inode)
  1597. return -ENOSPC;
  1598. error = security_inode_init_security(inode, dir, NULL, NULL,
  1599. NULL);
  1600. if (error) {
  1601. if (error != -EOPNOTSUPP) {
  1602. iput(inode);
  1603. return error;
  1604. }
  1605. error = 0;
  1606. }
  1607. info = SHMEM_I(inode);
  1608. inode->i_size = len-1;
  1609. if (len <= (char *)inode - (char *)info) {
  1610. /* do it inline */
  1611. memcpy(info, symname, len);
  1612. inode->i_op = &shmem_symlink_inline_operations;
  1613. } else {
  1614. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1615. if (error) {
  1616. iput(inode);
  1617. return error;
  1618. }
  1619. inode->i_op = &shmem_symlink_inode_operations;
  1620. kaddr = kmap_atomic(page, KM_USER0);
  1621. memcpy(kaddr, symname, len);
  1622. kunmap_atomic(kaddr, KM_USER0);
  1623. set_page_dirty(page);
  1624. page_cache_release(page);
  1625. }
  1626. if (dir->i_mode & S_ISGID)
  1627. inode->i_gid = dir->i_gid;
  1628. dir->i_size += BOGO_DIRENT_SIZE;
  1629. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1630. d_instantiate(dentry, inode);
  1631. dget(dentry);
  1632. return 0;
  1633. }
  1634. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1635. {
  1636. nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
  1637. return NULL;
  1638. }
  1639. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1640. {
  1641. struct page *page = NULL;
  1642. int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1643. nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
  1644. return page;
  1645. }
  1646. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1647. {
  1648. if (!IS_ERR(nd_get_link(nd))) {
  1649. struct page *page = cookie;
  1650. kunmap(page);
  1651. mark_page_accessed(page);
  1652. page_cache_release(page);
  1653. }
  1654. }
  1655. static struct inode_operations shmem_symlink_inline_operations = {
  1656. .readlink = generic_readlink,
  1657. .follow_link = shmem_follow_link_inline,
  1658. };
  1659. static struct inode_operations shmem_symlink_inode_operations = {
  1660. .truncate = shmem_truncate,
  1661. .readlink = generic_readlink,
  1662. .follow_link = shmem_follow_link,
  1663. .put_link = shmem_put_link,
  1664. };
  1665. static int shmem_parse_options(char *options, int *mode, uid_t *uid,
  1666. gid_t *gid, unsigned long *blocks, unsigned long *inodes,
  1667. int *policy, nodemask_t *policy_nodes)
  1668. {
  1669. char *this_char, *value, *rest;
  1670. while ((this_char = strsep(&options, ",")) != NULL) {
  1671. if (!*this_char)
  1672. continue;
  1673. if ((value = strchr(this_char,'=')) != NULL) {
  1674. *value++ = 0;
  1675. } else {
  1676. printk(KERN_ERR
  1677. "tmpfs: No value for mount option '%s'\n",
  1678. this_char);
  1679. return 1;
  1680. }
  1681. if (!strcmp(this_char,"size")) {
  1682. unsigned long long size;
  1683. size = memparse(value,&rest);
  1684. if (*rest == '%') {
  1685. size <<= PAGE_SHIFT;
  1686. size *= totalram_pages;
  1687. do_div(size, 100);
  1688. rest++;
  1689. }
  1690. if (*rest)
  1691. goto bad_val;
  1692. *blocks = size >> PAGE_CACHE_SHIFT;
  1693. } else if (!strcmp(this_char,"nr_blocks")) {
  1694. *blocks = memparse(value,&rest);
  1695. if (*rest)
  1696. goto bad_val;
  1697. } else if (!strcmp(this_char,"nr_inodes")) {
  1698. *inodes = memparse(value,&rest);
  1699. if (*rest)
  1700. goto bad_val;
  1701. } else if (!strcmp(this_char,"mode")) {
  1702. if (!mode)
  1703. continue;
  1704. *mode = simple_strtoul(value,&rest,8);
  1705. if (*rest)
  1706. goto bad_val;
  1707. } else if (!strcmp(this_char,"uid")) {
  1708. if (!uid)
  1709. continue;
  1710. *uid = simple_strtoul(value,&rest,0);
  1711. if (*rest)
  1712. goto bad_val;
  1713. } else if (!strcmp(this_char,"gid")) {
  1714. if (!gid)
  1715. continue;
  1716. *gid = simple_strtoul(value,&rest,0);
  1717. if (*rest)
  1718. goto bad_val;
  1719. } else if (!strcmp(this_char,"mpol")) {
  1720. if (!strcmp(value,"default"))
  1721. *policy = MPOL_DEFAULT;
  1722. else if (!strcmp(value,"preferred"))
  1723. *policy = MPOL_PREFERRED;
  1724. else if (!strcmp(value,"bind"))
  1725. *policy = MPOL_BIND;
  1726. else if (!strcmp(value,"interleave"))
  1727. *policy = MPOL_INTERLEAVE;
  1728. else
  1729. goto bad_val;
  1730. } else if (!strcmp(this_char,"mpol_nodelist")) {
  1731. nodelist_parse(value, *policy_nodes);
  1732. } else {
  1733. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1734. this_char);
  1735. return 1;
  1736. }
  1737. }
  1738. return 0;
  1739. bad_val:
  1740. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1741. value, this_char);
  1742. return 1;
  1743. }
  1744. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1745. {
  1746. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1747. unsigned long max_blocks = sbinfo->max_blocks;
  1748. unsigned long max_inodes = sbinfo->max_inodes;
  1749. int policy = sbinfo->policy;
  1750. nodemask_t policy_nodes = sbinfo->policy_nodes;
  1751. unsigned long blocks;
  1752. unsigned long inodes;
  1753. int error = -EINVAL;
  1754. if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
  1755. &max_inodes, &policy, &policy_nodes))
  1756. return error;
  1757. spin_lock(&sbinfo->stat_lock);
  1758. blocks = sbinfo->max_blocks - sbinfo->free_blocks;
  1759. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1760. if (max_blocks < blocks)
  1761. goto out;
  1762. if (max_inodes < inodes)
  1763. goto out;
  1764. /*
  1765. * Those tests also disallow limited->unlimited while any are in
  1766. * use, so i_blocks will always be zero when max_blocks is zero;
  1767. * but we must separately disallow unlimited->limited, because
  1768. * in that case we have no record of how much is already in use.
  1769. */
  1770. if (max_blocks && !sbinfo->max_blocks)
  1771. goto out;
  1772. if (max_inodes && !sbinfo->max_inodes)
  1773. goto out;
  1774. error = 0;
  1775. sbinfo->max_blocks = max_blocks;
  1776. sbinfo->free_blocks = max_blocks - blocks;
  1777. sbinfo->max_inodes = max_inodes;
  1778. sbinfo->free_inodes = max_inodes - inodes;
  1779. sbinfo->policy = policy;
  1780. sbinfo->policy_nodes = policy_nodes;
  1781. out:
  1782. spin_unlock(&sbinfo->stat_lock);
  1783. return error;
  1784. }
  1785. #endif
  1786. static void shmem_put_super(struct super_block *sb)
  1787. {
  1788. kfree(sb->s_fs_info);
  1789. sb->s_fs_info = NULL;
  1790. }
  1791. static int shmem_fill_super(struct super_block *sb,
  1792. void *data, int silent)
  1793. {
  1794. struct inode *inode;
  1795. struct dentry *root;
  1796. int mode = S_IRWXUGO | S_ISVTX;
  1797. uid_t uid = current->fsuid;
  1798. gid_t gid = current->fsgid;
  1799. int err = -ENOMEM;
  1800. struct shmem_sb_info *sbinfo;
  1801. unsigned long blocks = 0;
  1802. unsigned long inodes = 0;
  1803. int policy = MPOL_DEFAULT;
  1804. nodemask_t policy_nodes = node_online_map;
  1805. #ifdef CONFIG_TMPFS
  1806. /*
  1807. * Per default we only allow half of the physical ram per
  1808. * tmpfs instance, limiting inodes to one per page of lowmem;
  1809. * but the internal instance is left unlimited.
  1810. */
  1811. if (!(sb->s_flags & MS_NOUSER)) {
  1812. blocks = totalram_pages / 2;
  1813. inodes = totalram_pages - totalhigh_pages;
  1814. if (inodes > blocks)
  1815. inodes = blocks;
  1816. if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
  1817. &inodes, &policy, &policy_nodes))
  1818. return -EINVAL;
  1819. }
  1820. #else
  1821. sb->s_flags |= MS_NOUSER;
  1822. #endif
  1823. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1824. sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
  1825. L1_CACHE_BYTES), GFP_KERNEL);
  1826. if (!sbinfo)
  1827. return -ENOMEM;
  1828. spin_lock_init(&sbinfo->stat_lock);
  1829. sbinfo->max_blocks = blocks;
  1830. sbinfo->free_blocks = blocks;
  1831. sbinfo->max_inodes = inodes;
  1832. sbinfo->free_inodes = inodes;
  1833. sbinfo->policy = policy;
  1834. sbinfo->policy_nodes = policy_nodes;
  1835. sb->s_fs_info = sbinfo;
  1836. sb->s_maxbytes = SHMEM_MAX_BYTES;
  1837. sb->s_blocksize = PAGE_CACHE_SIZE;
  1838. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1839. sb->s_magic = TMPFS_MAGIC;
  1840. sb->s_op = &shmem_ops;
  1841. inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
  1842. if (!inode)
  1843. goto failed;
  1844. inode->i_uid = uid;
  1845. inode->i_gid = gid;
  1846. root = d_alloc_root(inode);
  1847. if (!root)
  1848. goto failed_iput;
  1849. sb->s_root = root;
  1850. return 0;
  1851. failed_iput:
  1852. iput(inode);
  1853. failed:
  1854. shmem_put_super(sb);
  1855. return err;
  1856. }
  1857. static kmem_cache_t *shmem_inode_cachep;
  1858. static struct inode *shmem_alloc_inode(struct super_block *sb)
  1859. {
  1860. struct shmem_inode_info *p;
  1861. p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
  1862. if (!p)
  1863. return NULL;
  1864. return &p->vfs_inode;
  1865. }
  1866. static void shmem_destroy_inode(struct inode *inode)
  1867. {
  1868. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  1869. /* only struct inode is valid if it's an inline symlink */
  1870. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  1871. }
  1872. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  1873. }
  1874. static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
  1875. {
  1876. struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
  1877. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1878. SLAB_CTOR_CONSTRUCTOR) {
  1879. inode_init_once(&p->vfs_inode);
  1880. }
  1881. }
  1882. static int init_inodecache(void)
  1883. {
  1884. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  1885. sizeof(struct shmem_inode_info),
  1886. 0, 0, init_once, NULL);
  1887. if (shmem_inode_cachep == NULL)
  1888. return -ENOMEM;
  1889. return 0;
  1890. }
  1891. static void destroy_inodecache(void)
  1892. {
  1893. if (kmem_cache_destroy(shmem_inode_cachep))
  1894. printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
  1895. }
  1896. static struct address_space_operations shmem_aops = {
  1897. .writepage = shmem_writepage,
  1898. .set_page_dirty = __set_page_dirty_nobuffers,
  1899. #ifdef CONFIG_TMPFS
  1900. .prepare_write = shmem_prepare_write,
  1901. .commit_write = simple_commit_write,
  1902. #endif
  1903. };
  1904. static struct file_operations shmem_file_operations = {
  1905. .mmap = shmem_mmap,
  1906. #ifdef CONFIG_TMPFS
  1907. .llseek = generic_file_llseek,
  1908. .read = shmem_file_read,
  1909. .write = shmem_file_write,
  1910. .fsync = simple_sync_file,
  1911. .sendfile = shmem_file_sendfile,
  1912. #endif
  1913. };
  1914. static struct inode_operations shmem_inode_operations = {
  1915. .truncate = shmem_truncate,
  1916. .setattr = shmem_notify_change,
  1917. .truncate_range = shmem_truncate_range,
  1918. };
  1919. static struct inode_operations shmem_dir_inode_operations = {
  1920. #ifdef CONFIG_TMPFS
  1921. .create = shmem_create,
  1922. .lookup = simple_lookup,
  1923. .link = shmem_link,
  1924. .unlink = shmem_unlink,
  1925. .symlink = shmem_symlink,
  1926. .mkdir = shmem_mkdir,
  1927. .rmdir = shmem_rmdir,
  1928. .mknod = shmem_mknod,
  1929. .rename = shmem_rename,
  1930. #endif
  1931. };
  1932. static struct super_operations shmem_ops = {
  1933. .alloc_inode = shmem_alloc_inode,
  1934. .destroy_inode = shmem_destroy_inode,
  1935. #ifdef CONFIG_TMPFS
  1936. .statfs = shmem_statfs,
  1937. .remount_fs = shmem_remount_fs,
  1938. #endif
  1939. .delete_inode = shmem_delete_inode,
  1940. .drop_inode = generic_delete_inode,
  1941. .put_super = shmem_put_super,
  1942. };
  1943. static struct vm_operations_struct shmem_vm_ops = {
  1944. .nopage = shmem_nopage,
  1945. .populate = shmem_populate,
  1946. #ifdef CONFIG_NUMA
  1947. .set_policy = shmem_set_policy,
  1948. .get_policy = shmem_get_policy,
  1949. #endif
  1950. };
  1951. static struct super_block *shmem_get_sb(struct file_system_type *fs_type,
  1952. int flags, const char *dev_name, void *data)
  1953. {
  1954. return get_sb_nodev(fs_type, flags, data, shmem_fill_super);
  1955. }
  1956. static struct file_system_type tmpfs_fs_type = {
  1957. .owner = THIS_MODULE,
  1958. .name = "tmpfs",
  1959. .get_sb = shmem_get_sb,
  1960. .kill_sb = kill_litter_super,
  1961. };
  1962. static struct vfsmount *shm_mnt;
  1963. static int __init init_tmpfs(void)
  1964. {
  1965. int error;
  1966. error = init_inodecache();
  1967. if (error)
  1968. goto out3;
  1969. error = register_filesystem(&tmpfs_fs_type);
  1970. if (error) {
  1971. printk(KERN_ERR "Could not register tmpfs\n");
  1972. goto out2;
  1973. }
  1974. #ifdef CONFIG_TMPFS
  1975. devfs_mk_dir("shm");
  1976. #endif
  1977. shm_mnt = do_kern_mount(tmpfs_fs_type.name, MS_NOUSER,
  1978. tmpfs_fs_type.name, NULL);
  1979. if (IS_ERR(shm_mnt)) {
  1980. error = PTR_ERR(shm_mnt);
  1981. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  1982. goto out1;
  1983. }
  1984. return 0;
  1985. out1:
  1986. unregister_filesystem(&tmpfs_fs_type);
  1987. out2:
  1988. destroy_inodecache();
  1989. out3:
  1990. shm_mnt = ERR_PTR(error);
  1991. return error;
  1992. }
  1993. module_init(init_tmpfs)
  1994. /*
  1995. * shmem_file_setup - get an unlinked file living in tmpfs
  1996. *
  1997. * @name: name for dentry (to be seen in /proc/<pid>/maps
  1998. * @size: size to be set for the file
  1999. *
  2000. */
  2001. struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
  2002. {
  2003. int error;
  2004. struct file *file;
  2005. struct inode *inode;
  2006. struct dentry *dentry, *root;
  2007. struct qstr this;
  2008. if (IS_ERR(shm_mnt))
  2009. return (void *)shm_mnt;
  2010. if (size < 0 || size > SHMEM_MAX_BYTES)
  2011. return ERR_PTR(-EINVAL);
  2012. if (shmem_acct_size(flags, size))
  2013. return ERR_PTR(-ENOMEM);
  2014. error = -ENOMEM;
  2015. this.name = name;
  2016. this.len = strlen(name);
  2017. this.hash = 0; /* will go */
  2018. root = shm_mnt->mnt_root;
  2019. dentry = d_alloc(root, &this);
  2020. if (!dentry)
  2021. goto put_memory;
  2022. error = -ENFILE;
  2023. file = get_empty_filp();
  2024. if (!file)
  2025. goto put_dentry;
  2026. error = -ENOSPC;
  2027. inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
  2028. if (!inode)
  2029. goto close_file;
  2030. SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
  2031. d_instantiate(dentry, inode);
  2032. inode->i_size = size;
  2033. inode->i_nlink = 0; /* It is unlinked */
  2034. file->f_vfsmnt = mntget(shm_mnt);
  2035. file->f_dentry = dentry;
  2036. file->f_mapping = inode->i_mapping;
  2037. file->f_op = &shmem_file_operations;
  2038. file->f_mode = FMODE_WRITE | FMODE_READ;
  2039. return file;
  2040. close_file:
  2041. put_filp(file);
  2042. put_dentry:
  2043. dput(dentry);
  2044. put_memory:
  2045. shmem_unacct_size(flags, size);
  2046. return ERR_PTR(error);
  2047. }
  2048. /*
  2049. * shmem_zero_setup - setup a shared anonymous mapping
  2050. *
  2051. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2052. */
  2053. int shmem_zero_setup(struct vm_area_struct *vma)
  2054. {
  2055. struct file *file;
  2056. loff_t size = vma->vm_end - vma->vm_start;
  2057. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2058. if (IS_ERR(file))
  2059. return PTR_ERR(file);
  2060. if (vma->vm_file)
  2061. fput(vma->vm_file);
  2062. vma->vm_file = file;
  2063. vma->vm_ops = &shmem_vm_ops;
  2064. return 0;
  2065. }