ipc: remove the ipc_get() routine
[sfrench/cifs-2.6.git] / ipc / shm.c
1 /*
2  * linux/ipc/shm.c
3  * Copyright (C) 1992, 1993 Krishna Balasubramanian
4  *       Many improvements/fixes by Bruno Haible.
5  * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
6  * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
7  *
8  * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9  * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
10  * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
11  * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
12  * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
13  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
14  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
15  *
16  * support for audit of ipc object properties and permission changes
17  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
18  *
19  * namespaces support
20  * OpenVZ, SWsoft Inc.
21  * Pavel Emelianov <xemul@openvz.org>
22  */
23
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/hugetlb.h>
27 #include <linux/shm.h>
28 #include <linux/init.h>
29 #include <linux/file.h>
30 #include <linux/mman.h>
31 #include <linux/shmem_fs.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/audit.h>
35 #include <linux/capability.h>
36 #include <linux/ptrace.h>
37 #include <linux/seq_file.h>
38 #include <linux/mutex.h>
39 #include <linux/nsproxy.h>
40 #include <linux/mount.h>
41
42 #include <asm/uaccess.h>
43
44 #include "util.h"
45
46 struct shm_file_data {
47         int id;
48         struct ipc_namespace *ns;
49         struct file *file;
50         const struct vm_operations_struct *vm_ops;
51 };
52
53 #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
54
55 static const struct file_operations shm_file_operations;
56 static struct vm_operations_struct shm_vm_ops;
57
58 static struct ipc_ids init_shm_ids;
59
60 #define shm_ids(ns)     (*((ns)->ids[IPC_SHM_IDS]))
61
62 #define shm_lock(ns, id)                \
63         ((struct shmid_kernel*)ipc_lock(&shm_ids(ns),id))
64 #define shm_unlock(shp)                 \
65         ipc_unlock(&(shp)->shm_perm)
66 #define shm_buildid(ns, id, seq)        \
67         ipc_buildid(&shm_ids(ns), id, seq)
68
69 static int newseg(struct ipc_namespace *, struct ipc_params *);
70 static void shm_open(struct vm_area_struct *vma);
71 static void shm_close(struct vm_area_struct *vma);
72 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
73 #ifdef CONFIG_PROC_FS
74 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
75 #endif
76
77 static void __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
78 {
79         ns->ids[IPC_SHM_IDS] = ids;
80         ns->shm_ctlmax = SHMMAX;
81         ns->shm_ctlall = SHMALL;
82         ns->shm_ctlmni = SHMMNI;
83         ns->shm_tot = 0;
84         ipc_init_ids(ids);
85 }
86
87 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
88 {
89         if (shp->shm_nattch){
90                 shp->shm_perm.mode |= SHM_DEST;
91                 /* Do not find it any more */
92                 shp->shm_perm.key = IPC_PRIVATE;
93                 shm_unlock(shp);
94         } else
95                 shm_destroy(ns, shp);
96 }
97
98 int shm_init_ns(struct ipc_namespace *ns)
99 {
100         struct ipc_ids *ids;
101
102         ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
103         if (ids == NULL)
104                 return -ENOMEM;
105
106         __shm_init_ns(ns, ids);
107         return 0;
108 }
109
110 void shm_exit_ns(struct ipc_namespace *ns)
111 {
112         struct shmid_kernel *shp;
113         int next_id;
114         int total, in_use;
115
116         mutex_lock(&shm_ids(ns).mutex);
117
118         in_use = shm_ids(ns).in_use;
119
120         for (total = 0, next_id = 0; total < in_use; next_id++) {
121                 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
122                 if (shp == NULL)
123                         continue;
124                 ipc_lock_by_ptr(&shp->shm_perm);
125                 do_shm_rmid(ns, shp);
126                 total++;
127         }
128         mutex_unlock(&shm_ids(ns).mutex);
129
130         kfree(ns->ids[IPC_SHM_IDS]);
131         ns->ids[IPC_SHM_IDS] = NULL;
132 }
133
134 void __init shm_init (void)
135 {
136         __shm_init_ns(&init_ipc_ns, &init_shm_ids);
137         ipc_init_proc_interface("sysvipc/shm",
138                                 "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n",
139                                 IPC_SHM_IDS, sysvipc_shm_proc_show);
140 }
141
142 static inline int shm_checkid(struct ipc_namespace *ns,
143                 struct shmid_kernel *s, int id)
144 {
145         if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
146                 return -EIDRM;
147         return 0;
148 }
149
150 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
151 {
152         ipc_rmid(&shm_ids(ns), &s->shm_perm);
153 }
154
155 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
156 {
157         return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
158 }
159
160
161
162 /* This is called by fork, once for every shm attach. */
163 static void shm_open(struct vm_area_struct *vma)
164 {
165         struct file *file = vma->vm_file;
166         struct shm_file_data *sfd = shm_file_data(file);
167         struct shmid_kernel *shp;
168
169         shp = shm_lock(sfd->ns, sfd->id);
170         BUG_ON(!shp);
171         shp->shm_atim = get_seconds();
172         shp->shm_lprid = task_tgid_vnr(current);
173         shp->shm_nattch++;
174         shm_unlock(shp);
175 }
176
177 /*
178  * shm_destroy - free the struct shmid_kernel
179  *
180  * @shp: struct to free
181  *
182  * It has to be called with shp and shm_ids.mutex locked,
183  * but returns with shp unlocked and freed.
184  */
185 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
186 {
187         ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
188         shm_rmid(ns, shp);
189         shm_unlock(shp);
190         if (!is_file_hugepages(shp->shm_file))
191                 shmem_lock(shp->shm_file, 0, shp->mlock_user);
192         else
193                 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
194                                                 shp->mlock_user);
195         fput (shp->shm_file);
196         security_shm_free(shp);
197         ipc_rcu_putref(shp);
198 }
199
200 /*
201  * remove the attach descriptor vma.
202  * free memory for segment if it is marked destroyed.
203  * The descriptor has already been removed from the current->mm->mmap list
204  * and will later be kfree()d.
205  */
206 static void shm_close(struct vm_area_struct *vma)
207 {
208         struct file * file = vma->vm_file;
209         struct shm_file_data *sfd = shm_file_data(file);
210         struct shmid_kernel *shp;
211         struct ipc_namespace *ns = sfd->ns;
212
213         mutex_lock(&shm_ids(ns).mutex);
214         /* remove from the list of attaches of the shm segment */
215         shp = shm_lock(ns, sfd->id);
216         BUG_ON(!shp);
217         shp->shm_lprid = task_tgid_vnr(current);
218         shp->shm_dtim = get_seconds();
219         shp->shm_nattch--;
220         if(shp->shm_nattch == 0 &&
221            shp->shm_perm.mode & SHM_DEST)
222                 shm_destroy(ns, shp);
223         else
224                 shm_unlock(shp);
225         mutex_unlock(&shm_ids(ns).mutex);
226 }
227
228 static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
229 {
230         struct file *file = vma->vm_file;
231         struct shm_file_data *sfd = shm_file_data(file);
232
233         return sfd->vm_ops->fault(vma, vmf);
234 }
235
236 #ifdef CONFIG_NUMA
237 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
238 {
239         struct file *file = vma->vm_file;
240         struct shm_file_data *sfd = shm_file_data(file);
241         int err = 0;
242         if (sfd->vm_ops->set_policy)
243                 err = sfd->vm_ops->set_policy(vma, new);
244         return err;
245 }
246
247 static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
248                                         unsigned long addr)
249 {
250         struct file *file = vma->vm_file;
251         struct shm_file_data *sfd = shm_file_data(file);
252         struct mempolicy *pol = NULL;
253
254         if (sfd->vm_ops->get_policy)
255                 pol = sfd->vm_ops->get_policy(vma, addr);
256         else if (vma->vm_policy)
257                 pol = vma->vm_policy;
258         else
259                 pol = current->mempolicy;
260         return pol;
261 }
262 #endif
263
264 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
265 {
266         struct shm_file_data *sfd = shm_file_data(file);
267         int ret;
268
269         ret = sfd->file->f_op->mmap(sfd->file, vma);
270         if (ret != 0)
271                 return ret;
272         sfd->vm_ops = vma->vm_ops;
273 #ifdef CONFIG_MMU
274         BUG_ON(!sfd->vm_ops->fault);
275 #endif
276         vma->vm_ops = &shm_vm_ops;
277         shm_open(vma);
278
279         return ret;
280 }
281
282 static int shm_release(struct inode *ino, struct file *file)
283 {
284         struct shm_file_data *sfd = shm_file_data(file);
285
286         put_ipc_ns(sfd->ns);
287         shm_file_data(file) = NULL;
288         kfree(sfd);
289         return 0;
290 }
291
292 static int shm_fsync(struct file *file, struct dentry *dentry, int datasync)
293 {
294         int (*fsync) (struct file *, struct dentry *, int datasync);
295         struct shm_file_data *sfd = shm_file_data(file);
296         int ret = -EINVAL;
297
298         fsync = sfd->file->f_op->fsync;
299         if (fsync)
300                 ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync);
301         return ret;
302 }
303
304 static unsigned long shm_get_unmapped_area(struct file *file,
305         unsigned long addr, unsigned long len, unsigned long pgoff,
306         unsigned long flags)
307 {
308         struct shm_file_data *sfd = shm_file_data(file);
309         return get_unmapped_area(sfd->file, addr, len, pgoff, flags);
310 }
311
312 int is_file_shm_hugepages(struct file *file)
313 {
314         int ret = 0;
315
316         if (file->f_op == &shm_file_operations) {
317                 struct shm_file_data *sfd;
318                 sfd = shm_file_data(file);
319                 ret = is_file_hugepages(sfd->file);
320         }
321         return ret;
322 }
323
324 static const struct file_operations shm_file_operations = {
325         .mmap           = shm_mmap,
326         .fsync          = shm_fsync,
327         .release        = shm_release,
328         .get_unmapped_area      = shm_get_unmapped_area,
329 };
330
331 static struct vm_operations_struct shm_vm_ops = {
332         .open   = shm_open,     /* callback for a new vm-area open */
333         .close  = shm_close,    /* callback for when the vm-area is released */
334         .fault  = shm_fault,
335 #if defined(CONFIG_NUMA)
336         .set_policy = shm_set_policy,
337         .get_policy = shm_get_policy,
338 #endif
339 };
340
341 static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
342 {
343         key_t key = params->key;
344         int shmflg = params->flg;
345         size_t size = params->u.size;
346         int error;
347         struct shmid_kernel *shp;
348         int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
349         struct file * file;
350         char name[13];
351         int id;
352
353         if (size < SHMMIN || size > ns->shm_ctlmax)
354                 return -EINVAL;
355
356         if (ns->shm_tot + numpages > ns->shm_ctlall)
357                 return -ENOSPC;
358
359         shp = ipc_rcu_alloc(sizeof(*shp));
360         if (!shp)
361                 return -ENOMEM;
362
363         shp->shm_perm.key = key;
364         shp->shm_perm.mode = (shmflg & S_IRWXUGO);
365         shp->mlock_user = NULL;
366
367         shp->shm_perm.security = NULL;
368         error = security_shm_alloc(shp);
369         if (error) {
370                 ipc_rcu_putref(shp);
371                 return error;
372         }
373
374         sprintf (name, "SYSV%08x", key);
375         if (shmflg & SHM_HUGETLB) {
376                 /* hugetlb_file_setup takes care of mlock user accounting */
377                 file = hugetlb_file_setup(name, size);
378                 shp->mlock_user = current->user;
379         } else {
380                 int acctflag = VM_ACCOUNT;
381                 /*
382                  * Do not allow no accounting for OVERCOMMIT_NEVER, even
383                  * if it's asked for.
384                  */
385                 if  ((shmflg & SHM_NORESERVE) &&
386                                 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
387                         acctflag = 0;
388                 file = shmem_file_setup(name, size, acctflag);
389         }
390         error = PTR_ERR(file);
391         if (IS_ERR(file))
392                 goto no_file;
393
394         error = -ENOSPC;
395         id = shm_addid(ns, shp);
396         if(id == -1) 
397                 goto no_id;
398
399         shp->shm_cprid = task_tgid_vnr(current);
400         shp->shm_lprid = 0;
401         shp->shm_atim = shp->shm_dtim = 0;
402         shp->shm_ctim = get_seconds();
403         shp->shm_segsz = size;
404         shp->shm_nattch = 0;
405         shp->shm_perm.id = shm_buildid(ns, id, shp->shm_perm.seq);
406         shp->shm_file = file;
407         /*
408          * shmid gets reported as "inode#" in /proc/pid/maps.
409          * proc-ps tools use this. Changing this will break them.
410          */
411         file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
412
413         ns->shm_tot += numpages;
414         error = shp->shm_perm.id;
415         shm_unlock(shp);
416         return error;
417
418 no_id:
419         fput(file);
420 no_file:
421         security_shm_free(shp);
422         ipc_rcu_putref(shp);
423         return error;
424 }
425
426 static inline int shm_security(void *shp, int shmflg)
427 {
428         return security_shm_associate((struct shmid_kernel *) shp, shmflg);
429 }
430
431 static inline int shm_more_checks(void *shp, struct ipc_params *params)
432 {
433         if (((struct shmid_kernel *)shp)->shm_segsz < params->u.size)
434                 return -EINVAL;
435
436         return 0;
437 }
438
439 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
440 {
441         struct ipc_namespace *ns;
442         struct ipc_ops shm_ops;
443         struct ipc_params shm_params;
444
445         ns = current->nsproxy->ipc_ns;
446
447         shm_ops.getnew = newseg;
448         shm_ops.associate = shm_security;
449         shm_ops.more_checks = shm_more_checks;
450
451         shm_params.key = key;
452         shm_params.flg = shmflg;
453         shm_params.u.size = size;
454
455         return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
456 }
457
458 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
459 {
460         switch(version) {
461         case IPC_64:
462                 return copy_to_user(buf, in, sizeof(*in));
463         case IPC_OLD:
464             {
465                 struct shmid_ds out;
466
467                 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
468                 out.shm_segsz   = in->shm_segsz;
469                 out.shm_atime   = in->shm_atime;
470                 out.shm_dtime   = in->shm_dtime;
471                 out.shm_ctime   = in->shm_ctime;
472                 out.shm_cpid    = in->shm_cpid;
473                 out.shm_lpid    = in->shm_lpid;
474                 out.shm_nattch  = in->shm_nattch;
475
476                 return copy_to_user(buf, &out, sizeof(out));
477             }
478         default:
479                 return -EINVAL;
480         }
481 }
482
483 struct shm_setbuf {
484         uid_t   uid;
485         gid_t   gid;
486         mode_t  mode;
487 };      
488
489 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
490 {
491         switch(version) {
492         case IPC_64:
493             {
494                 struct shmid64_ds tbuf;
495
496                 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
497                         return -EFAULT;
498
499                 out->uid        = tbuf.shm_perm.uid;
500                 out->gid        = tbuf.shm_perm.gid;
501                 out->mode       = tbuf.shm_perm.mode;
502
503                 return 0;
504             }
505         case IPC_OLD:
506             {
507                 struct shmid_ds tbuf_old;
508
509                 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
510                         return -EFAULT;
511
512                 out->uid        = tbuf_old.shm_perm.uid;
513                 out->gid        = tbuf_old.shm_perm.gid;
514                 out->mode       = tbuf_old.shm_perm.mode;
515
516                 return 0;
517             }
518         default:
519                 return -EINVAL;
520         }
521 }
522
523 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
524 {
525         switch(version) {
526         case IPC_64:
527                 return copy_to_user(buf, in, sizeof(*in));
528         case IPC_OLD:
529             {
530                 struct shminfo out;
531
532                 if(in->shmmax > INT_MAX)
533                         out.shmmax = INT_MAX;
534                 else
535                         out.shmmax = (int)in->shmmax;
536
537                 out.shmmin      = in->shmmin;
538                 out.shmmni      = in->shmmni;
539                 out.shmseg      = in->shmseg;
540                 out.shmall      = in->shmall; 
541
542                 return copy_to_user(buf, &out, sizeof(out));
543             }
544         default:
545                 return -EINVAL;
546         }
547 }
548
549 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
550                 unsigned long *swp)
551 {
552         int next_id;
553         int total, in_use;
554
555         *rss = 0;
556         *swp = 0;
557
558         in_use = shm_ids(ns).in_use;
559
560         for (total = 0, next_id = 0; total < in_use; next_id++) {
561                 struct shmid_kernel *shp;
562                 struct inode *inode;
563
564                 /*
565                  * idr_find() is called via shm_get(), so with shm_ids.mutex
566                  * locked. Since ipc_addid() is also called with
567                  * shm_ids.mutex down, there is no need to add read barriers
568                  * here to gurantee the writes in ipc_addid() are seen in
569                  * order here (for Alpha).
570                  * However idr_find() itself does not necessary require
571                  * ipc_ids.mutex down. So if idr_find() is used by other
572                  * places without ipc_ids.mutex down, then it needs read
573                  * read memory barriers as ipc_lock() does.
574                  */
575
576                 shp = idr_find(&shm_ids(ns).ipcs_idr, next_id);
577                 if (shp == NULL)
578                         continue;
579
580                 inode = shp->shm_file->f_path.dentry->d_inode;
581
582                 if (is_file_hugepages(shp->shm_file)) {
583                         struct address_space *mapping = inode->i_mapping;
584                         *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
585                 } else {
586                         struct shmem_inode_info *info = SHMEM_I(inode);
587                         spin_lock(&info->lock);
588                         *rss += inode->i_mapping->nrpages;
589                         *swp += info->swapped;
590                         spin_unlock(&info->lock);
591                 }
592
593                 total++;
594         }
595 }
596
597 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
598 {
599         struct shm_setbuf setbuf;
600         struct shmid_kernel *shp;
601         int err, version;
602         struct ipc_namespace *ns;
603
604         if (cmd < 0 || shmid < 0) {
605                 err = -EINVAL;
606                 goto out;
607         }
608
609         version = ipc_parse_version(&cmd);
610         ns = current->nsproxy->ipc_ns;
611
612         switch (cmd) { /* replace with proc interface ? */
613         case IPC_INFO:
614         {
615                 struct shminfo64 shminfo;
616
617                 err = security_shm_shmctl(NULL, cmd);
618                 if (err)
619                         return err;
620
621                 memset(&shminfo,0,sizeof(shminfo));
622                 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
623                 shminfo.shmmax = ns->shm_ctlmax;
624                 shminfo.shmall = ns->shm_ctlall;
625
626                 shminfo.shmmin = SHMMIN;
627                 if(copy_shminfo_to_user (buf, &shminfo, version))
628                         return -EFAULT;
629                 /* reading a integer is always atomic */
630                 err = ipc_get_maxid(&shm_ids(ns));
631                 if(err<0)
632                         err = 0;
633                 goto out;
634         }
635         case SHM_INFO:
636         {
637                 struct shm_info shm_info;
638
639                 err = security_shm_shmctl(NULL, cmd);
640                 if (err)
641                         return err;
642
643                 memset(&shm_info,0,sizeof(shm_info));
644                 mutex_lock(&shm_ids(ns).mutex);
645                 shm_info.used_ids = shm_ids(ns).in_use;
646                 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
647                 shm_info.shm_tot = ns->shm_tot;
648                 shm_info.swap_attempts = 0;
649                 shm_info.swap_successes = 0;
650                 err = ipc_get_maxid(&shm_ids(ns));
651                 mutex_unlock(&shm_ids(ns).mutex);
652                 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
653                         err = -EFAULT;
654                         goto out;
655                 }
656
657                 err = err < 0 ? 0 : err;
658                 goto out;
659         }
660         case SHM_STAT:
661         case IPC_STAT:
662         {
663                 struct shmid64_ds tbuf;
664                 int result;
665                 memset(&tbuf, 0, sizeof(tbuf));
666                 shp = shm_lock(ns, shmid);
667                 if(shp==NULL) {
668                         err = -EINVAL;
669                         goto out;
670                 } else if (cmd == SHM_STAT) {
671                         result = shp->shm_perm.id;
672                 } else {
673                         err = shm_checkid(ns, shp,shmid);
674                         if(err)
675                                 goto out_unlock;
676                         result = 0;
677                 }
678                 err=-EACCES;
679                 if (ipcperms (&shp->shm_perm, S_IRUGO))
680                         goto out_unlock;
681                 err = security_shm_shmctl(shp, cmd);
682                 if (err)
683                         goto out_unlock;
684                 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
685                 tbuf.shm_segsz  = shp->shm_segsz;
686                 tbuf.shm_atime  = shp->shm_atim;
687                 tbuf.shm_dtime  = shp->shm_dtim;
688                 tbuf.shm_ctime  = shp->shm_ctim;
689                 tbuf.shm_cpid   = shp->shm_cprid;
690                 tbuf.shm_lpid   = shp->shm_lprid;
691                 tbuf.shm_nattch = shp->shm_nattch;
692                 shm_unlock(shp);
693                 if(copy_shmid_to_user (buf, &tbuf, version))
694                         err = -EFAULT;
695                 else
696                         err = result;
697                 goto out;
698         }
699         case SHM_LOCK:
700         case SHM_UNLOCK:
701         {
702                 shp = shm_lock(ns, shmid);
703                 if(shp==NULL) {
704                         err = -EINVAL;
705                         goto out;
706                 }
707                 err = shm_checkid(ns, shp,shmid);
708                 if(err)
709                         goto out_unlock;
710
711                 err = audit_ipc_obj(&(shp->shm_perm));
712                 if (err)
713                         goto out_unlock;
714
715                 if (!capable(CAP_IPC_LOCK)) {
716                         err = -EPERM;
717                         if (current->euid != shp->shm_perm.uid &&
718                             current->euid != shp->shm_perm.cuid)
719                                 goto out_unlock;
720                         if (cmd == SHM_LOCK &&
721                             !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
722                                 goto out_unlock;
723                 }
724
725                 err = security_shm_shmctl(shp, cmd);
726                 if (err)
727                         goto out_unlock;
728                 
729                 if(cmd==SHM_LOCK) {
730                         struct user_struct * user = current->user;
731                         if (!is_file_hugepages(shp->shm_file)) {
732                                 err = shmem_lock(shp->shm_file, 1, user);
733                                 if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
734                                         shp->shm_perm.mode |= SHM_LOCKED;
735                                         shp->mlock_user = user;
736                                 }
737                         }
738                 } else if (!is_file_hugepages(shp->shm_file)) {
739                         shmem_lock(shp->shm_file, 0, shp->mlock_user);
740                         shp->shm_perm.mode &= ~SHM_LOCKED;
741                         shp->mlock_user = NULL;
742                 }
743                 shm_unlock(shp);
744                 goto out;
745         }
746         case IPC_RMID:
747         {
748                 /*
749                  *      We cannot simply remove the file. The SVID states
750                  *      that the block remains until the last person
751                  *      detaches from it, then is deleted. A shmat() on
752                  *      an RMID segment is legal in older Linux and if 
753                  *      we change it apps break...
754                  *
755                  *      Instead we set a destroyed flag, and then blow
756                  *      the name away when the usage hits zero.
757                  */
758                 mutex_lock(&shm_ids(ns).mutex);
759                 shp = shm_lock(ns, shmid);
760                 err = -EINVAL;
761                 if (shp == NULL) 
762                         goto out_up;
763                 err = shm_checkid(ns, shp, shmid);
764                 if(err)
765                         goto out_unlock_up;
766
767                 err = audit_ipc_obj(&(shp->shm_perm));
768                 if (err)
769                         goto out_unlock_up;
770
771                 if (current->euid != shp->shm_perm.uid &&
772                     current->euid != shp->shm_perm.cuid && 
773                     !capable(CAP_SYS_ADMIN)) {
774                         err=-EPERM;
775                         goto out_unlock_up;
776                 }
777
778                 err = security_shm_shmctl(shp, cmd);
779                 if (err)
780                         goto out_unlock_up;
781
782                 do_shm_rmid(ns, shp);
783                 mutex_unlock(&shm_ids(ns).mutex);
784                 goto out;
785         }
786
787         case IPC_SET:
788         {
789                 if (copy_shmid_from_user (&setbuf, buf, version)) {
790                         err = -EFAULT;
791                         goto out;
792                 }
793                 mutex_lock(&shm_ids(ns).mutex);
794                 shp = shm_lock(ns, shmid);
795                 err=-EINVAL;
796                 if(shp==NULL)
797                         goto out_up;
798                 err = shm_checkid(ns, shp,shmid);
799                 if(err)
800                         goto out_unlock_up;
801                 err = audit_ipc_obj(&(shp->shm_perm));
802                 if (err)
803                         goto out_unlock_up;
804                 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
805                 if (err)
806                         goto out_unlock_up;
807                 err=-EPERM;
808                 if (current->euid != shp->shm_perm.uid &&
809                     current->euid != shp->shm_perm.cuid && 
810                     !capable(CAP_SYS_ADMIN)) {
811                         goto out_unlock_up;
812                 }
813
814                 err = security_shm_shmctl(shp, cmd);
815                 if (err)
816                         goto out_unlock_up;
817                 
818                 shp->shm_perm.uid = setbuf.uid;
819                 shp->shm_perm.gid = setbuf.gid;
820                 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
821                         | (setbuf.mode & S_IRWXUGO);
822                 shp->shm_ctim = get_seconds();
823                 break;
824         }
825
826         default:
827                 err = -EINVAL;
828                 goto out;
829         }
830
831         err = 0;
832 out_unlock_up:
833         shm_unlock(shp);
834 out_up:
835         mutex_unlock(&shm_ids(ns).mutex);
836         goto out;
837 out_unlock:
838         shm_unlock(shp);
839 out:
840         return err;
841 }
842
843 /*
844  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
845  *
846  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
847  * "raddr" thing points to kernel space, and there has to be a wrapper around
848  * this.
849  */
850 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
851 {
852         struct shmid_kernel *shp;
853         unsigned long addr;
854         unsigned long size;
855         struct file * file;
856         int    err;
857         unsigned long flags;
858         unsigned long prot;
859         int acc_mode;
860         unsigned long user_addr;
861         struct ipc_namespace *ns;
862         struct shm_file_data *sfd;
863         struct path path;
864         mode_t f_mode;
865
866         err = -EINVAL;
867         if (shmid < 0)
868                 goto out;
869         else if ((addr = (ulong)shmaddr)) {
870                 if (addr & (SHMLBA-1)) {
871                         if (shmflg & SHM_RND)
872                                 addr &= ~(SHMLBA-1);       /* round down */
873                         else
874 #ifndef __ARCH_FORCE_SHMLBA
875                                 if (addr & ~PAGE_MASK)
876 #endif
877                                         goto out;
878                 }
879                 flags = MAP_SHARED | MAP_FIXED;
880         } else {
881                 if ((shmflg & SHM_REMAP))
882                         goto out;
883
884                 flags = MAP_SHARED;
885         }
886
887         if (shmflg & SHM_RDONLY) {
888                 prot = PROT_READ;
889                 acc_mode = S_IRUGO;
890                 f_mode = FMODE_READ;
891         } else {
892                 prot = PROT_READ | PROT_WRITE;
893                 acc_mode = S_IRUGO | S_IWUGO;
894                 f_mode = FMODE_READ | FMODE_WRITE;
895         }
896         if (shmflg & SHM_EXEC) {
897                 prot |= PROT_EXEC;
898                 acc_mode |= S_IXUGO;
899         }
900
901         /*
902          * We cannot rely on the fs check since SYSV IPC does have an
903          * additional creator id...
904          */
905         ns = current->nsproxy->ipc_ns;
906         shp = shm_lock(ns, shmid);
907         if(shp == NULL)
908                 goto out;
909
910         err = shm_checkid(ns, shp,shmid);
911         if (err)
912                 goto out_unlock;
913
914         err = -EACCES;
915         if (ipcperms(&shp->shm_perm, acc_mode))
916                 goto out_unlock;
917
918         err = security_shm_shmat(shp, shmaddr, shmflg);
919         if (err)
920                 goto out_unlock;
921
922         path.dentry = dget(shp->shm_file->f_path.dentry);
923         path.mnt    = shp->shm_file->f_path.mnt;
924         shp->shm_nattch++;
925         size = i_size_read(path.dentry->d_inode);
926         shm_unlock(shp);
927
928         err = -ENOMEM;
929         sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
930         if (!sfd)
931                 goto out_put_dentry;
932
933         err = -ENOMEM;
934
935         file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
936         if (!file)
937                 goto out_free;
938
939         file->private_data = sfd;
940         file->f_mapping = shp->shm_file->f_mapping;
941         sfd->id = shp->shm_perm.id;
942         sfd->ns = get_ipc_ns(ns);
943         sfd->file = shp->shm_file;
944         sfd->vm_ops = NULL;
945
946         down_write(&current->mm->mmap_sem);
947         if (addr && !(shmflg & SHM_REMAP)) {
948                 err = -EINVAL;
949                 if (find_vma_intersection(current->mm, addr, addr + size))
950                         goto invalid;
951                 /*
952                  * If shm segment goes below stack, make sure there is some
953                  * space left for the stack to grow (at least 4 pages).
954                  */
955                 if (addr < current->mm->start_stack &&
956                     addr > current->mm->start_stack - size - PAGE_SIZE * 5)
957                         goto invalid;
958         }
959                 
960         user_addr = do_mmap (file, addr, size, prot, flags, 0);
961         *raddr = user_addr;
962         err = 0;
963         if (IS_ERR_VALUE(user_addr))
964                 err = (long)user_addr;
965 invalid:
966         up_write(&current->mm->mmap_sem);
967
968         fput(file);
969
970 out_nattch:
971         mutex_lock(&shm_ids(ns).mutex);
972         shp = shm_lock(ns, shmid);
973         BUG_ON(!shp);
974         shp->shm_nattch--;
975         if(shp->shm_nattch == 0 &&
976            shp->shm_perm.mode & SHM_DEST)
977                 shm_destroy(ns, shp);
978         else
979                 shm_unlock(shp);
980         mutex_unlock(&shm_ids(ns).mutex);
981
982 out:
983         return err;
984
985 out_unlock:
986         shm_unlock(shp);
987         goto out;
988
989 out_free:
990         kfree(sfd);
991 out_put_dentry:
992         dput(path.dentry);
993         goto out_nattch;
994 }
995
996 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
997 {
998         unsigned long ret;
999         long err;
1000
1001         err = do_shmat(shmid, shmaddr, shmflg, &ret);
1002         if (err)
1003                 return err;
1004         force_successful_syscall_return();
1005         return (long)ret;
1006 }
1007
1008 /*
1009  * detach and kill segment if marked destroyed.
1010  * The work is done in shm_close.
1011  */
1012 asmlinkage long sys_shmdt(char __user *shmaddr)
1013 {
1014         struct mm_struct *mm = current->mm;
1015         struct vm_area_struct *vma, *next;
1016         unsigned long addr = (unsigned long)shmaddr;
1017         loff_t size = 0;
1018         int retval = -EINVAL;
1019
1020         if (addr & ~PAGE_MASK)
1021                 return retval;
1022
1023         down_write(&mm->mmap_sem);
1024
1025         /*
1026          * This function tries to be smart and unmap shm segments that
1027          * were modified by partial mlock or munmap calls:
1028          * - It first determines the size of the shm segment that should be
1029          *   unmapped: It searches for a vma that is backed by shm and that
1030          *   started at address shmaddr. It records it's size and then unmaps
1031          *   it.
1032          * - Then it unmaps all shm vmas that started at shmaddr and that
1033          *   are within the initially determined size.
1034          * Errors from do_munmap are ignored: the function only fails if
1035          * it's called with invalid parameters or if it's called to unmap
1036          * a part of a vma. Both calls in this function are for full vmas,
1037          * the parameters are directly copied from the vma itself and always
1038          * valid - therefore do_munmap cannot fail. (famous last words?)
1039          */
1040         /*
1041          * If it had been mremap()'d, the starting address would not
1042          * match the usual checks anyway. So assume all vma's are
1043          * above the starting address given.
1044          */
1045         vma = find_vma(mm, addr);
1046
1047         while (vma) {
1048                 next = vma->vm_next;
1049
1050                 /*
1051                  * Check if the starting address would match, i.e. it's
1052                  * a fragment created by mprotect() and/or munmap(), or it
1053                  * otherwise it starts at this address with no hassles.
1054                  */
1055                 if ((vma->vm_ops == &shm_vm_ops) &&
1056                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1057
1058
1059                         size = vma->vm_file->f_path.dentry->d_inode->i_size;
1060                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1061                         /*
1062                          * We discovered the size of the shm segment, so
1063                          * break out of here and fall through to the next
1064                          * loop that uses the size information to stop
1065                          * searching for matching vma's.
1066                          */
1067                         retval = 0;
1068                         vma = next;
1069                         break;
1070                 }
1071                 vma = next;
1072         }
1073
1074         /*
1075          * We need look no further than the maximum address a fragment
1076          * could possibly have landed at. Also cast things to loff_t to
1077          * prevent overflows and make comparisions vs. equal-width types.
1078          */
1079         size = PAGE_ALIGN(size);
1080         while (vma && (loff_t)(vma->vm_end - addr) <= size) {
1081                 next = vma->vm_next;
1082
1083                 /* finding a matching vma now does not alter retval */
1084                 if ((vma->vm_ops == &shm_vm_ops) &&
1085                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
1086
1087                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1088                 vma = next;
1089         }
1090
1091         up_write(&mm->mmap_sem);
1092         return retval;
1093 }
1094
1095 #ifdef CONFIG_PROC_FS
1096 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1097 {
1098         struct shmid_kernel *shp = it;
1099         char *format;
1100
1101 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1102 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1103
1104         if (sizeof(size_t) <= sizeof(int))
1105                 format = SMALL_STRING;
1106         else
1107                 format = BIG_STRING;
1108         return seq_printf(s, format,
1109                           shp->shm_perm.key,
1110                           shp->shm_perm.id,
1111                           shp->shm_perm.mode,
1112                           shp->shm_segsz,
1113                           shp->shm_cprid,
1114                           shp->shm_lprid,
1115                           shp->shm_nattch,
1116                           shp->shm_perm.uid,
1117                           shp->shm_perm.gid,
1118                           shp->shm_perm.cuid,
1119                           shp->shm_perm.cgid,
1120                           shp->shm_atim,
1121                           shp->shm_dtim,
1122                           shp->shm_ctim);
1123 }
1124 #endif